• 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!

GameMaker how to do stuff every time an animation updates

C

Capptianm

Guest
I thought that the Animation_Update event to do just what it said on the box. I thought this event would activate every time an objects image index changes. It turns out that this event is only meant for skeletal animation. is there any way to activate some code when the image index updates?
 

FrostyCat

Redemption Seeker
Create:
Code:
image_index_previous = image_index;
End Step:
Code:
if (image_index_previous != image_index) {
  /* Perform actions here */
  image_index_previous = image_index;
}
 
C

Capptianm

Guest
this
Create:
Code:
image_index_previous = image_index;
End Step:
Code:
if (image_index_previous != image_index) {
  /* Perform actions here */
  image_index_previous = image_index;
}
this doesn't exactly work, it constantly activates the code. since the code plays an audio file, it plays that audio file every second. any other options?
 

MeBoingus

Member
The way that code works is:

Every time the frame (image_index) changes - the code is triggered.

Could you better describe what you expected/need to happen?
 
N

NeZvers

Guest
I think I know what's the problem.
image_index updates by image_speed each frame and so @FrostyCat suggested code will fire each frame. So if you need on each sprite image_index change do this:
Code:
if (image_index_previous != floor(image_index)) {
  /* Perform actions here */
  image_index_previous =floor(image_index);
}
 

MeBoingus

Member
I think I know what's the problem.
image_index updates by image_speed each frame and so @FrostyCat suggested code will fire each frame. So if you need on each sprite image_index change do this:
Code:
if (image_index_previous != floor(image_index)) {
  /* Perform actions here */
  image_index_previous =floor(image_index);
}

This would indeed do the trick. If you DO need to do anything with the areas 'between' frames, here's some info:

Frames are actually decimals.

Let's say we have a sprite with 4 frames.
If your image speed is 1.5, and you're on frame 0, this is what happens:

Image index 1.4 is considered (by the engine) to be image index 1. It will show the first frame.
However, image index 1.5 is rounded up to 2, and will thus show the 2nd frame.

The reason using 'floor' works here, is it rounds the numbers down, so that we're not working with decimals.
Otherwise that code would trigger every frame (1, 1.1, 1.2, etc).

That code above will only trigger whenever the frame changes by a whole number (1, 2, 3, etc).
 
N

NeZvers

Guest
This would indeed do the trick. If you DO need to do anything with the areas 'between' frames, here's some info:

Frames are actually decimals.

Let's say we have a sprite with 4 frames.
If your image speed is 1.5, and you're on frame 0, this is what happens:

Image index 1.4 is considered (by the engine) to be image index 1. It will show the first frame.
However, image index 1.5 is rounded up to 2, and will thus show the 2nd frame.

The reason using 'floor' works here, is it rounds the numbers down, so that we're not working with decimals.
Otherwise that code would trigger every frame (1, 1.1, 1.2, etc).

That code above will only trigger whenever the frame changes by a whole number (1, 2, 3, etc).
I don't think there's rounding to native image_index but floor rounding, hence I suggested to use it.
If there's 0.5 rounding up, then it means 0th image_index will be shown for half supposed length and last one 1.5x it's supposed length.
 
Top