• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

I need help with attack animation.

W

WimpyLlama

Guest
I made an attack script in Game Maker 2, but whenever I click left click (which is the button I set for attack) it only plays the first frame of the sprite animation. I have spent hours on this and I looked on google but I couldn't find anything. Please help.

mouse_left = mouse_check_button_pressed(mb_left);

//Attack
if (mouse_left) {

instance_create_layer(x+sign(hsp)*24,y,"Player",obj_hitbox);
image_speed = 1;
sprite_index = spr_player_attack;

}
 
W

WimpyLlama

Guest
Yes, but I have messed with those ones and they don't seem to be the thing affecting it. I think it's just because I am using keyboard_check_pressed. But if I don't use that, then the attack goes on forever if I hold it down, and I don't want that.
 

JackTurbo

Member
Well yes and no. It is sort of because you're using key_board_check_pressed, but also not really.

The condition is only true for one frame, which is probably what you want. However where ever you're setting sprite_index will be overriding the attack animation on the next frame.

There are a whole bunch of ways to approach this, but really what you want (I assume) is for the attack animation to play start-finish before reverting to what ever other animation should be playing (eg idle, walk/run, what ever).

Personally I (along with many other in the GMC) have a bit of an obsession with state machines so that is the approach I tend to take.

You could however declare a simple boolean variable like isAttacking = false to control when to play the animation, if you're not too familiar with statemachines. Like so:
Create Event
Code:
isAttacking = false;
Step Event
Code:
mouse_left = mouse_check_button_pressed(mb_left);
//Attack
if (mouse_left) {
    instance_create_layer(x+sign(hsp)*24,y,"Player",obj_hitbox);
    image_speed = 1;
    image_index = 0; //reset image index so that the attack animation will play from the start
    isAttacking = true; //set the is attacking boolean to true
   
}
if(isAttacking == true){
    sprite_index = spr_player_attack; //continually sets the sprite index to the attack sprite so long as boolean is true
}
Animation End Event
Code:
if(isAttacking == true){
    isAttacking = false;  //once animation is finished reset the attack boolean to false
}
 

Yal

🐧 *penguin noises*
GMC Elder
Yes, but I have messed with those ones and they don't seem to be the thing affecting it.
It very likely is - if the sprite only has 1 subimage, image_index will be clamped to the first image, even if you change the sprite back later. This is super-easy to get tripped up on (I still do it all the time).
 
W

WimpyLlama

Guest
Thank you, this worked perfectly. One question, is there a way to make something wait, like:

if (key_down) {
grv = 0.6;
wait 3 sec;
grv = 0.2;
}
 

NightFrost

Member
You can use alarms to do countdowns like that.
Code:
// STEP
if(key_down){
    grv = 0.6;
    alarm[0] = room_speed * 3; // Equals three seconds.
}

// AMARM 0
grv = 0.2;
When you set an object's alarm to a non-zero value it counts down by one every step. Upon reaching zero, the alarm code runs. In case of this code, alarm 0 is repeatedly set to room_speed * 3 for as long as the key is held down, so it actually starts counting down once the key is released. Pressing the key again while countdown is running sets it back up to three seconds.
 
W

WimpyLlama

Guest
Another thing, I created an enemy that keep falling through the floor. It worked just fine until I change a small value. Then it started falling through the floor, so I changed the value back and it is still falling. I don't know what the problem is, please help.
 

Yal

🐧 *penguin noises*
GMC Elder
Another thing, I created an enemy that keep falling through the floor. It worked just fine until I change a small value. Then it started falling through the floor, so I changed the value back and it is still falling. I don't know what the problem is, please help.
If you undid your change, you should get the same result as before, so you did something wrong.
 
Top