Not logged inGosu Forums
Forum back to libgosu.org Help Search Register Login
Up Topic Gosu / Gosu Exchange / How do I put a delay in Animation?
- - By iBloodLust Date 2015-11-15 22:50
hello, I've been trying for the past few hours trying to put a delay between each frame in my animation class in ruby gosu.
I need this to configure animation speed. here is the class:

class Animation
  def initialize(_AnimFile, x, y, z)
    @x = x
    @y = y
    @z = z
    @AnimStrip = Gosu::Image.load_tiles(_AnimFile,x,y)
    @AnimIndex = 0
  end
  def draw(loop)
    if @AnimIndex < @AnimStrip.count
      @AnimStrip[@AnimIndex].draw(@x, @y, @z)
      @AnimIndex += 1
    elsif loop == true
      @AnimIndex = 0
    end
  end
end
Parent - - By arrow Date 2015-11-16 02:15 Edited 2015-11-16 02:22
First of all I would seperate the logic and the drawing into an update and draw function. To change the speed you can simply add another number than 1 to the @AnimIndex.

Also I separated tile size and position for load_tiles and draw

class Animation
  def initialize(_AnimFile, x, y, z, tile_width, tile_height, loop, anim_index_delta=1.0)
    @x = x
    @y = y
    @z = z
    @AnimStrip = Gosu::Image.load_tiles(_AnimFile,tile_width,tile_height)
    @AnimIndex = 0
   
    # 1.0 is full speed
    # 0.5 is half speed
    # 2.0 is double speed
    @AnimIndexDelta = anim_index_delta
    @loop = loop
  end
 
  def update
    @AnimIndex += @AnimIndexDelta
   
    if @loop
      @AnimIndex = @AnimIndex % @AnimStrip.length
    else
      last_index = @AnimStrip.length-1
      @AnimIndex = last_index if @AnimIndex > last_index
    end
  end
 
  def draw
    @AnimStrip[@AnimIndex].draw(@x, @y, @z)
  end
end


For looping I'm using modulus, the %, for example modulus 12 works like a 12 hour clock. Try to start up IRB and enter: 16 % 12
# => 4


I hope this is enough to get you going.
Parent - By iBloodLust Date 2015-11-16 04:16
oh thank you! and you also gotten rid of the animation flashing everytime the animation loops.
Parent - By jlnr (dev) Date 2015-11-16 09:01
I would also suggest using @lower_case for variables and instance variables. UpperCase is only used for constants in Ruby.
Up Topic Gosu / Gosu Exchange / How do I put a delay in Animation?

Powered by mwForum 2.29.7 © 1999-2015 Markus Wichitill