SOLVED How i do to make an animation executes once when i press a key.

Hi, i am using Game Maker Studio 2.3, and i want to know how can i make an animation executes enterely when i press once time a key.
I did it when you keep pressing the key the animation keeps repeating itself, but i dont know how to do it pressing once the key, for example, "D" and the sprite makes the animation once time only.
Feel free to ask whatever you need, and thanks for reading.
 

Joe Ellis

Member
The animation end event is really useful for stuff like this.
Say this is the code for starting the animation:

GML:
if keyboard_check_pressed(ord("D"))
{
sprite_index = spr_player_attack
image_index = 0 //it's important to set this to 0 so it starts at the start of the animation
}
Then in the animation end event:

GML:
if sprite_index = spr_player_attack
{
sprite_index = spr_player_idle
}
Or maybe make a "previous_sprite" variable, set it to sprite_index just before changing, then change it back in the animation end event.
 
I have a problem with that, i have made 3 scripts. 1st for the key detection, 2nd for the key comprobation and the 3rd is for the sprite changing. I made 3 scripts because, later, i want to make a cooldown between attacks.

1st Script:
GML:
function scr_keys(){
right = keyboard_check_pressed(ord("D"));
left = keyboard_check_pressed(ord("A"));
}
2nd Script (Ignore the AttackCD variable, is for the comprobation of the cooldown)
GML:
function scr_keycheck(){
if left && !right// && !AttackCD
{
rightattack = false;
leftattack = true;

//AttackCD = true;

}

if right && !left// && !AttackCD
{
rightattack = true;
leftattack = false;

//AttackCD = true;


}
if !right && !left
{
 leftattack = false;
 rightattack = false;
// AttackCD = AttackCD;
}

}
3rd Script (Ignore the AttackCD again):
GML:
function scr_setsprite(){

if  leftattack{// && AttackCD
image_xscale = -1
sprite_index = charlie_attack

}

if rightattack { //&& AttackCD
image_xscale = 1
sprite_index = charlie_attack

}

if !rightattack && !leftattack {
    sprite_index = charlie_stand

}
}
 

TheouAegis

Member
So in your animation end event, clear leftattack and rightattack. But you then need to make sure neither is set before you check left and right.
 
This maybe is a newbie question, but, what is exactly animation and event? Rlly sorry if this question is too silly, but im learning yet.
 

Mr Magnus

Viking King
The animation end event is an event that triggers every time the instance in question completes an animation: i.e has played all the frames in its sprite and is about to start from the beginning.

You can pick it as one of the options in the same place as you'd pick the rest of the object events, such as "create" or "step" or "press W-key" or whatnot. I think it may be filed under other.
 
I tried to do something doing this, but it doesn't work. Any ideas?
GML:
function scr_setsprite(){

if  leftattack{// && AttackCD
image_xscale = -1
sprite_index = charlie_attack
    image_index = 0;
}

if rightattack { //&& AttackCD
image_xscale = 1
sprite_index = charlie_attack
    image_index = 0;
}
if !rightattack && !leftattack {
sprite_index = charlie_stand

}

if sprite_index == charlie_attack{
    sprite_index = charlie_stand
}


}
 

woods

Member
would something like this work?
Code:
if  leftattack{// && AttackCD
image_xscale = -1
sprite_index = charlie_attack
    image_index = 0;
 if (image_index+image_speed >= image_number) {
image_index = charlie_stand
}
}
 
would something like this work?
Code:
if  leftattack{// && AttackCD
image_xscale = -1
sprite_index = charlie_attack
    image_index = 0;
if (image_index+image_speed >= image_number) {
image_index = charlie_stand
}
}
This make some progress, but it doesnt work at all. I included in the script like this
GML:
function scr_setsprite(){

if  leftattack{// && AttackCD
image_xscale = -1
sprite_index = charlie_attack;
image_index = 0;
image_speed = 1;
if (image_index+image_speed >= image_number) {
image_index = charlie_stand;
}
}

if rightattack { //&& AttackCD
   
image_xscale = 1;
sprite_index = charlie_attack;
image_index = 0;
image_speed = 1;
if (image_index+image_speed >= image_number) {
image_index = charlie_stand;
}

}
if !rightattack && !leftattack {
sprite_index = charlie_stand;

}




}
And what this does is, when you press "D" or "A", its just shows the first frame of the attack sprite, and then comes back to the stand sprite. So, i need to know how to make the animation reproduce enterely.
 

Nidoking

Member
You're setting your sprite back to charlie_stand if you're not pressing either attack button. You probably want to not do that.

Also
image_index = charlie_stand;
This makes no sense.

Actually, this whole thing isn't going to work.
image_index = 0; image_speed = 1; if (image_index+image_speed >= image_number)
image_index + image_speed will always equal 1. I don't think it's possible for a sprite to have less than one frame.
 
You're setting your sprite back to charlie_stand if you're not pressing either attack button. You probably want to not do that.

Also

This makes no sense.

Actually, this whole thing isn't going to work.

image_index + image_speed will always equal 1. I don't think it's possible for a sprite to have less than one frame.
Ok. So, what do you think that i can do?
 

Nidoking

Member
You're setting your sprite back to charlie_stand if you're not pressing either attack button. You probably want to not do that.
You haven't posted anything to indicate that you've used the Animation End Event as you were told to do. You can't ask for the next thing to do until you've done the thing before the next thing.
 

woods

Member
image_index = charlie_stand;
This makes no sense.

my fault .. typo supposed to be sprite_index ;/



but yeah..like everyone above has been saying, changing the state at the end of animation will work
 
Top