samples/03_rendering_sprites/01_animation_using_separate_pngs/app/main.rb
# This function shows how to animate a sprite that executes
# only once when the "f" key is pressed.
This function shows how to animate a sprite that executes
only once when the "f" key is pressed.
This function shows how to animate a sprite that executes only once when the " f " key is pressed .
def one_time_animation args
  args.outputs.labels <<  { x: 220, y: 350, text: "(press f to animate)" }
  if args.inputs.keyboard.key_down.f
    puts "Hello from main.rb! The \"f\" key was in the down state on frame: #{args.state.tick_count}"
    args.state.start_looping_at = args.state.tick_count
  end
  number_of_sprites = 6
  number_of_frames_to_show_each_sprite = 4
  does_sprite_loop = false
  sprite_index = args.state
                     .start_looping_at
                     .frame_index number_of_sprites,
                                  number_of_frames_to_show_each_sprite,
                                  does_sprite_loop
  sprite_index ||= 0
  args.outputs.sprites << { x: 100, y: 300, w: 100, h: 100, path: "sprites/dragon_fly_#{sprite_index}.png" }
  tick_instructions args, "Sample app shows how to use Numeric#frame_index and string interpolation to animate a sprite over time."
end
def one_time_animation args args . outputs . labels << { x : 220 , y : 350 , text : "(press f to animate)" } if args . inputs . keyboard . key_down . f puts "Hello from main.rb! The \"f\" key was in the down state on frame: #{args.state.tick_count}" args . state . start_looping_at = args . state . tick_count end number_of_sprites = 6 number_of_frames_to_show_each_sprite = 4 does_sprite_loop = false sprite_index = args . state . start_looping_at . frame_index number_of_sprites , number_of_frames_to_show_each_sprite , does_sprite_loop sprite_index ||= 0 args . outputs . sprites << { x : 100 , y : 300 , w : 100 , h : 100 , path : "sprites/dragon_fly_#{sprite_index}.png" } tick_instructions args , "Sample app shows how to use Numeric#frame_index and string interpolation to animate a sprite over time." end
This function shows how to animate a sprite that executes
only once when the "f" key is pressed.
This function shows how to animate a sprite that executes only once when the " f " key is pressed .
# This is just a label the shows instructions within the game. # If "f" is pressed on the keyboard... # Print the frame that "f" was pressed on. # And MOST IMPORTANTLY set the point it time to start the animation, # equal to "now" which is represented as args.state.tick_count. # Also IMPORTANT, you'll notice that the value of when to start looping # is stored in `args.state`. This construct's values are retained across # executions of the `tick` method. # These are the same local variables that were defined # for the `looping_animation` function. # Except this sprite does not loop again. If the animation time has passed, # then the frame_index function returns nil. # This line sets the frame index to zero, if # the animation duration has passed (frame_index returned nil). # Remeber: we are not looping forever here. # Present the sprite.
args