Animation Events - KormexGit/GM-Animate GitHub Wiki
Animation events allow you to automatically run a piece of code every time a sprite hits specified frames. Adding an animation event only has to be done once, so you don't need to add code in step to continually check the currently active sprite and it's frame. This makes them ideal for anything you want to always happen for that sprite, such as playing a sound effect or spawning particles.
animation_event_add
Adds an animation event to this instance. This function should only be called a single time per animation event, so it's recommended to use it in the create event. The animation event will only be active when one of the sprites passed for the first argument is the currently active sprite on the track the animation event is added to. Each time the sprite hits one of the event frames, the callback function will be called.
- What's a callback?
A "callback" is a function that you pass as an argument to another function. In this case, you're giving the event a function containing the code you want to run when the animation event triggers.
- What's callback scope?
By default, the callback function you pass will run in the scope of the instance that called animation_event_add. This means you can use variables belonging to that instance directly. If you want to use or change the animation's variables inside the callback, you will need to access them the same way you would if you were running code directly on the instance, such as anim.image_angle
or animations[0].image_xscale
. If you do not want the callback to run for the calling instance, you can pass the instance or struct you do want it to be bound to for the _callback_scope
argument.
animation_event_add(_sprite, _frames, _callback, _callback_scope = self, _track = 0)
Argument | Type | Description |
---|---|---|
_sprite | Sprite Asset or Array of Sprite Assets | The sprite to add the event to. Can pass an array of sprites to add the event to all of them |
_frames | Real or Array of Reals | Which frame the event should trigger on. Can pass an array of frames to have the event trigger on multiple frames |
_callback | Function | A function containing the code you want to run when the event is triggered |
_callback_scope | Instance ID or Struct | (Optional) the scope the callback should run in. By default, it will be bound to the calling instance |
_track | Real (Int) | (Optional) The track to add the event to. Pass all to add the event to all existing tracks at once. Note: will only add the event to tracks that have already been initialized with animation_start |
Returns: Animation Event Struct
Animation Event Struct
animation_event_add returns an animation event struct that can be passed to the other event functions below. The animation event struct only contains two variables: frames
and callback
, which contain whatever you passed for the _frames and _callback arguments. Actually running the animation events happens when animation_run() is called.
animation_event_remove
Removes an animation event from the instance. To be able to remove an animation event, you will need to have stored the animation event struct returned by animation_event_add in a variable, and then pass that variable as the _event
argument.
animation_event_remove(_event, _track = 0)
Argument | Type | Description |
---|---|---|
_event | Event Struct | The event struct for the event you want to remove, as returned by animation_event_add |
_track | Real (Int) | (Optional) The track to remove the event from. Pass all to remove it from every track |
Returns: N/A (undefined)
animation_event_remove_all
Removes all events from the instance, for every sprite and every track.
animation_event_remove_all()
Returns: N/A (undefined)