ACTIONS & ANIMATIONS
The Animation class provides the frame by frame animations to Sprites. The frames should be retrieved using a Sprite Sheet and a sprite frame Cache for optimal performance.
SpriteFrameCache.frames.add file_name: 'animation.plist' flying_frames = SpriteFrameCache.frames.where prefix: 'ship', suffix: '.png' animation = Animation.new frames: flying_frames
In the previous example the sprite sheet plist file is loaded into the Sprite Frame Cache, and using its where method it retrieves an array of frames that will become the key-frames for the animation.
For more information, see: Sprite Frame Cache documentation page.
Run a Animation
The Animation cannot be run by itself in the Sprite, it needs to be wrapped into an Action. The following is the appropriate way of archiving this:
animation = Animation.new frames: flying_frames sprite.run_action animation.action
The Animation's action can be combined with other actions as well, like the Delay or Callback.
For more information, see: Delay documentation page.
For more information, see: Callback documentation page.
Returns a new instance of the Animation class:
Animation.new frames: animation_frames
delay_per_unit: - Float - Default: 0.1
Number of seconds of delay per unit.
delay_units: - Float - Default: The number of frames
Number of delay units in the Animation.
restore_original_frame: - Boolean
Should restore the original frame after completion?
loops: - Integer
How many times the animation is going to loop.
Delay per Unit
Retrieve or update the delay per unit:
animation.delay_per_unit = 0.5 # The following is 0.5 animation.delay_per_unit
Gets or sets the total delay units of the Animation:
animation.delay_units = 18 # The following is 18 animation.delay_units
Retrieves the Animation's duration:
animation.delay_per_unit = 0.2 animation.delay_units = 18 # The following is 3.6 animation.duration
The Animation duration is the product of Delay Units and Delay per Unit.
Gets or sets how many times the Animation is going to loop:
animation.loops = 2 # The following is 2 animation.loops
Restore Original Frame
Retrieves or updates if the Animation should restore the original frame:
animation.restore_original_frame = false # The following is false animation.restore_original_frame
Adds a new frame into the Animation:
animation.add_frame frame: SpriteFrameCache.frames animation.add_frame file_name: 'speed_of_light.png' animation.add_frame texture: mother_ship_texture, rect: [[0, 0], [200, 200]]
Returns the Animation wrapped into an Action:
This is the proper way to run an Animation into a Sprite.