Welcome, Guest. Please login or register.

Login with username, password and session length

 
Advanced search

1411722 Posts in 69402 Topics- by 58455 Members - Latest Member: Sergei

May 22, 2024, 07:56:21 AM

Need hosting? Check out Digital Ocean
(more details in this thread)
TIGSource ForumsDeveloperTechnical (Moderator: ThemsAllTook)"3 stepped attack" in GML
Pages: [1]
Print
Author Topic: "3 stepped attack" in GML  (Read 2047 times)
DeeTox
Level 0
*


View Profile
« on: May 21, 2010, 04:23:34 PM »

Hello, DeeTox here.

I recently started working on a Game Maker project I named "Woody's Adventure" (mentioned it during my introduction) and I now face a bit of a problem with getting a 3 stepped attack to work smoothly. Right now I have been able to jump through the steps from step one to step three in the attack, but I don't think I've used the best solution.

I have 3 individual sprites representing the different steps in the attack.
spr_atk_r_1, spr_atk_r_2, and spr_atk_r_3. These sprites have 8 subimages each (don't know if this really matters to you), and what I want to do is when I hit "z" (the attack button) he goes from standing still to the first step (spr_atk_r_1), after 5 of the subimages I want it to go to the second step if "z" is hit again. After another 5 subimages on the spr_atk_r_2 I want it to go to the third step if "z" is hit.

Here's the code that takes me through the steps like I want, but not in the smoothest way:
Code:
var atk;

atk=0;

if keyboard_check_pressed(ord('Z'))
{
 atk=1;
 sprite_index=spr_atk_r_1;
 image_index=0;
 image_speed=0.4;
 alarm+=1;

 if keyboard_check_pressed(ord('Z')) and atk==1 and alarm>=1
 {
 
  atk=2;
  sprite_index=spr_atk_r_2;
  image_index=0;
  image_speed=0.4;
 
 
  if keyboard_check_pressed(ord('Z')) and atk==2 and alarm>=2
  {
   atk=3;
   sprite_index=spr_atk_r_3;
   image_index=0;
   image_speed=0.4;
  }
 }
}

My question is; Are there any other way to solve this with less code and better result?
Logged
dariusk
Level 0
***



View Profile WWW
« Reply #1 on: May 22, 2010, 11:02:37 AM »

What I would do is run this code.

In the Create event:

Code:
atk = 0;

In the Animation End event:

Code:
atk = 0;
sprite_index = your_default_sprite;

In the Step event:

Code:
next_attack = false;

if keyboard_check_pressed(ord('Z') and atk == 0)
{
 atk=1;
 sprite_index=spr_atk_r_1;
 image_index=0;
 image_speed=0.4;
}

if (keyboard_check_pressed(ord('Z') and atk == 1 and image_index < 5)
{
 atk=2;
 next_attack = true;
}

if (next_attack and image_index == 5 and atk == 2)
{
 next_attack = false;
 sprite_index=spr_atk_r_2;
 image_index=0;
 image_speed=0.4;
}

if (keyboard_check_pressed(ord('Z') and atk == 2 and image_index < 5)
{
 atk=3;
 next_attack = true;
}

if (next_attack and image_index == 5 and atk == 3)
{
 next_attack = false;
 sprite_index=spr_atk_r_3;
 image_index=0;
 image_speed=0.4;
}

Here's what happens. The user presses Z which fires off the attack1 animation and sets atk = 1. If the user presses Z before the 5th frame (that image_index check I'm doing), it sets the "next_attack" variable to true. When atk = 1 and we're ON the 5th frame, it checks to see if next_attack is true (AKA if the person hit the Z button). If it's not true, they didn't hit the Z button, so the current animation keeps playing until it ends at frame 8 and sets it back to the default sprite, and the attack state is reset to 0. If it IS true, then they hit the Z button, so it immediately switches over to the attack2 animation and begins playing that one. I do the same check for animation 3.

It's more code but it'll probably end up a lot smoother. Also I haven't tested this in GM so there might be typos and stuff. But it should work.
Logged

DeeTox
Level 0
*


View Profile
« Reply #2 on: May 22, 2010, 12:37:49 PM »

Thank, I'll try this out. Don't know why I haven't thought about using true/false before. 

I'll get back to you if there's any problems with this solution, but looking at that code makes me pretty sure it won't.  Gentleman
Logged
dariusk
Level 0
***



View Profile WWW
« Reply #3 on: May 22, 2010, 12:39:22 PM »

Hey, let me know if it *does* work too Smiley
Logged

DeeTox
Level 0
*


View Profile
« Reply #4 on: May 22, 2010, 12:41:13 PM »

Haha, of course I will let you know :D
I have to thank you properly. This problem's been giving me the finger for about 2 days now^^
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic