samples/03_rendering_sprites/01_animation_using_separate_pngs/app/main.rb
# This function shows how to animate a sprite that loops forever.
This function shows how to animate a sprite that loops forever.
This function shows how to animate a sprite that loops forever .
def looping_animation args
  start_looping_at = 0
  number_of_sprites = 6
  number_of_frames_to_show_each_sprite = 4
  does_sprite_loop = true
  sprite_index = start_looping_at.frame_index number_of_sprites,
                                              number_of_frames_to_show_each_sprite,
                                              does_sprite_loop
  args.outputs.sprites << { x: 100, y: 100, w: 100, h: 100, path: "sprites/dragon_fly_#{sprite_index}.png" }
  args.outputs.sprites << { x: 100, y: 200, w: 100, h: 100, path: "sprites/dragon_fly_#{0.frame_index 6, 4, true}.png" }
end
def looping_animation args start_looping_at = 0 number_of_sprites = 6 number_of_frames_to_show_each_sprite = 4 does_sprite_loop = true sprite_index = start_looping_at . frame_index number_of_sprites , number_of_frames_to_show_each_sprite , does_sprite_loop args . outputs . sprites << { x : 100 , y : 100 , w : 100 , h : 100 , path : "sprites/dragon_fly_#{sprite_index}.png" } args . outputs . sprites << { x : 100 , y : 200 , w : 100 , h : 100 , path : "sprites/dragon_fly_#{0.frame_index 6, 4, true}.png" } end
This function shows how to animate a sprite that loops forever.
This function shows how to animate a sprite that loops forever .
# Here we define a few local variables that will be sent # into the magic function that gives us the correct sprite image # over time. There are four things we need in order to figure # out which sprite to show. # 1. When to start the animation. # 2. The number of pngs that represent the full animation. # 3. How long to show each png. # 4. Whether the animation should loop once, or forever. # With the variables defined above, we can get a number # which represents the sprite to show by calling the `frame_index` function. # In this case the number will be between 0, and 5 (you can see the sprites # in the ./sprites directory). # Now that we have `sprite_index, we can present the correct file. # Try changing the numbers below to see how the animation changes:
args