GameMaker [SOLVED] How to make an animation only play once

AcrobatEpee

Member
I have an animation I want to play once and then stop at the current position and change sprite, how may I proceed ? Thanks !
 
C

CedSharp

Guest
There is an event called Animation End which is called when the sprite is done animating all sub-images.


Inside that event, you can simply test for what sprite you currently have, and if it's the sprite which animation you wanted to play once, then change the sprite.

Using code, you would do something like this:
Code:
switch(sprite_index) {
  case spr_jump_from_ground:
    sprite_index = spr_jump_going_up;
    break;
  
  case spr_jump_landing:
    sprite_index = spr_idle;
    break;
}
The above code will change the sprite when the animation ends (if you put it in the 'Other > Animation End' event)
only if the sprite was "spr_jump_from_ground" or if it was "spr_jump_landing".

You can do the equivalent with drag n drop, I'm sure you can figure out how to use the "test for sprite" and the "else" control statements :)

Hope this was usefull
 

AcrobatEpee

Member
Made a bit of research (just went through the manual) and found this :

  • Image Index - The image index of the sprite assigned. This value gets the frame of the sprite assigned to the instance. The returned value will be between 0 and the number of images for the sprite, and note that if the image speed is not exactly one then the returned value can be a decimal.

I think that’s what you were referring to ? When I reach the last index I stop the animation right ?

I failed to understand the image speed, what is that referring to ?

Thanks for your help :)
 
C

CedSharp

Guest
In GameMaker, a sprite is basically 2 things:
- a collection of sub-images (it can be just one)
- a collision mask (used for, well, collisions)

Each object has a different set of variables to control how the said sprites should be displayed.

- sprite_index: This is a reference to the sprite itself. If you change this, it's the same as selecting a new sprite for your object in the dropdown of the object editor

- image_index: This is the sub-image that is displayed. it loops from 0 to the last image, and restarts from 0 and again and again (well, a loop basically)


If you wanted to control the animation yourself (which I don't suggest if you're just starting with this) you would need to care about those values.
But in your situation, all you need is the 'Animation End' event, which is located under 'Other'. You can add this event the same way you add the create or step event.

You can use simple DnD if you prefer, something like this would work:
 
B

Bayesian

Guest
Animation End is a actual event you can add to your objects


Alternatively you can also check the image_index against image_number - 1 manually with code if you prefer all your code in the step or draw events
 

AcrobatEpee

Member
So I assign my animation to an object. When my key is pressed I play the animation (I change the sprite) and to this object when the animation end event happens I set back the sprite to the idle one, am I right here ?

But if the idle one is 1 image, as soon as it will be set to this sprite, it will change again and this in a loop no ?
 
Last edited:

AcrobatEpee

Member
I tried this and this and it gives me this :/

I guess it is because when the animation ends it is not on the last but on the first image sprite, how may I do then because checking for animation end will just make me enter a loop (My animation sprite will end and my idle sprite will replace it but then my idle sprite will keep being over because there is only one image in it)

Thanks all for helping :)
 
C

CedSharp

Guest
I tried this and this and it gives me this :/

I guess it is because when the animation ends it is not on the last but on the first image sprite, how may I do then because checking for animation end will just make me enter a loop (My animation sprite will end and my idle sprite will replace it but then my idle sprite will keep being over because there is only one image in it)

Thanks all for helping :)
If you read the post I provided, I gave you a screenshot of how the Animation End event should look like.
There is no image_index no where in there. When the 'Animation End' event starts, the animation IS ALREADY COMPLETED so you don't need to check that.

You instead need to check which sprite's animation has ended. In your case you want to test if the sprite is equal to "sprite6" before changing the sprite and moving as you intend.

Here is what you should have based on your attempt:
 
H

Hyperspazz

Guest
There is an event called Animation End which is called when the sprite is done animating all sub-images.


Inside that event, you can simply test for what sprite you currently have, and if it's the sprite which animation you wanted to play once, then change the sprite.

Using code, you would do something like this:
Code:
switch(sprite_index) {
  case spr_jump_from_ground:
    sprite_index = spr_jump_going_up;
    break;
 
  case spr_jump_landing:
    sprite_index = spr_idle;
    break;
}
The above code will change the sprite when the animation ends (if you put it in the 'Other > Animation End' event)
only if the sprite was "spr_jump_from_ground" or if it was "spr_jump_landing".

You can do the equivalent with drag n drop, I'm sure you can figure out how to use the "test for sprite" and the "else" control statements :)

Hope this was usefull
Hello, I am new here and I saw this thread.

I am having similar problem. My ship I want to play the destroyed animation once hit and play it once, but I do not want to switch it to anything else. I want this to happen everytime your ship spawns and gets hit.. I am doing the asteroids game if that helps.
 
S

Silver_Mantis

Guest
Hello, I am new here and I saw this thread.

I am having similar problem. My ship I want to play the destroyed animation once hit and play it once, but I do not want to switch it to anything else. I want this to happen everytime your ship spawns and gets hit.. I am doing the asteroids game if that helps.
A simple solution to this is:
1. Create two sprites - One of your ship, and the other of the ship's destroy animation.
2. Create two objects - One using the sprite of your ship, and the other using the sprite of your ship's destroy animation.
3. When your ship is hit, in it's destroy event, create a instance of the ships destroy animation in its place.
Example
GML:
instance_create_layer(obj_Player_Ship.x,obj_Player_Ship.y,"Player_Layer",obj_Ship_Destroy);
4. In your ship destroy animation object, destroy the object after it's animation has played.
(Event>Other>Animation End)
GML:
instance_destroy();
And you're done. :p
 
H

Hyperspazz

Guest
A simple solution to this is:
1. Create two sprites - One of your ship, and the other of the ship's destroy animation.
2. Create two objects - One using the sprite of your ship, and the other using the sprite of your ship's destroy animation.
3. When your ship is hit, in it's destroy event, create a instance of the ships destroy animation in its place.
Example
GML:
instance_create_layer(obj_Player_Ship.x,obj_Player_Ship.y,"Player_Layer",obj_Ship_Destroy);
4. In your ship destroy animation object, destroy the object after it's animation has played.
(Event>Other>Animation End)
GML:
instance_destroy();
And you're done. :p
Thanks for getting back to me so quick. I appreciate your time and effort in helping. I had all of it done as I am doing the tutorial on Youtube. I exchanged the first half of your code with the one in the tutorial and did everything right. When I went to run it and crashed my ship into the asteroid it gave me an error. I changed the first part of the code back to the following:

repeat(1){
instance_create_layer(x,y,"Instances",obj_ship_destroyed);
}

That worked. I did put in however, the last bit of code to end animation that you showed above and it works perfectly. Thank you so much for your help. I am so new at this stuff but am I am enjoying this process.
 

Nidoking

Member
repeat(1)?

That's like saying "Perform the following steps" or the "Enjoy!" at the end of every recipe ever. It doesn't mean anything.
 
Top