Envision, Create, Share

Welcome to HBGames, a leading amateur game development forum and Discord server. All are welcome, and amongst our ranks you will find experts in their field from all aspects of video game design and development.

[Resolved] Picture Animations?

Status
Not open for further replies.
I need help making a picture animation in a Scene Here's all I have:


Code:
bitmap = RPG::Cache.picture("locker")
    cw = bitmap.width / 4
    ch = bitmap.height
    @face_frame = 0
    src_rect = Rect.new(@face_frame * 88, 0, 88, bitmap.height)
    #src_rect = Rect.new(0, 0, cw, ch)
    self.contents.blt(x - cw * 1.5, y - ch, bitmap, src_rect)

Anyone know how to Animate it?:(
 
* Supposing this bitmap has 4 animation frames in a row with the same width and height...

Make a sprite, set its bitmap as "locker" and set its src_rect property for the position that you want in your animation.

Suppose in some part of the script you initialized your sprite and bitmap:
Code:
def initialize
  @sprite = Sprite.new
  @sprite.bitmap = RPG::Cache.picture("locker")
  @timing = 4
  @counter = 0
  @pattern = 0
end
Where @sprite is the sprite that will show up the bitmap in the screen, @timing is the number of frames to wait until it goes to the next animation frame, and the @counter is to count the frames until it reach the limit at @timing and go to the next animation frame.

Now suppose you have an update method in this class. All you´ll do is to check if @counter reached the value at @timing. If it reached, reset @counter and change to the next animation frame. The new animation frame will be @pattern + 1, and in your case, as the limit of animation frames is 4, when it reaches that value (4-1=3, because it goes from 0 to 3) the @pattern will be reset to 0 (the initial animation frame). Look:
Code:
def update
  if @counter == @timing
    @counter = 0
    cw = @sprite.bitmap.width / 4
    ch = @sprite.bitmap.height
    if @pattern == 3
      @pattern = 0
    else
      @pattern += 1
    end
    pt = @pattern
    @sprite.src_rect.set(cw * pt, 0, cw, ch)
  else
    @counter += 1
  end
end
Simple as this ^^ (i guess...) That way you don´t need to call time-consuming bitmap methods.

Any question, just ask.
 
It doesn't work....

EDIT:::
I got it to show one Frame but the rest don't show....

Code:
def draw_locker_graphic
    if @sprite != nil
      @sprite = nil
    end
    @sprite = Sprite.new
    @sprite.bitmap = RPG::Cache.picture("locker")
    @timing = 4
    @counter = 0
    @pattern = 0
    if @counter == @timing
      @counter = 0
      cw = @sprite.bitmap.width / 4
      ch = @sprite.bitmap.height
      if @pattern == 3
        @pattern = 0
      else
        @pattern += 1
      end
      pt = @pattern
      @sprite.src_rect.set(cw * pt, 0, cw, ch)
    else
      cw = @sprite.bitmap.width / 4
      ch = @sprite.bitmap.height
      #if @pattern == 3
      #  @pattern = 0
      #else
      #  @pattern += 1
      #end
      @counter += 1
      pt = @pattern
      @sprite.src_rect.set(cw * pt, 0, cw, ch)
    end
  end
 
I use this to draw animated sprites on windows and such.

Code:
#==============================================================================
# ** Bitmap
#==============================================================================

class Bitmap
  #--------------------------------------------------------------------------
  # * Animated Sprites Options
  #--------------------------------------------------------------------------
  Anim_Sprite_Reset_Frames = 8
  Anim_Sprite_Frame_Width  = 4
  Anim_Sprite_Frame_Height = 4
  #--------------------------------------------------------------------------
  # * Scale Blt
  #--------------------------------------------------------------------------
  def scale_blt(dest_rect, src_bitmap, 
      src_rect = Rect.new(0, 0, src_bitmap.width, src_bitmap.height), opacity = 255)
    w, h = src_rect.width, src_rect.height
    scale = [w / dest_rect.width.to_f, h / dest_rect.height.to_f].max
    ow, oh = (w / scale).to_i, (h / scale).to_i
    ox, oy = (dest_rect.width - ow) / 2, (dest_rect.height - oh) / 2
    stretch_blt(Rect.new(ox + dest_rect.x, oy + dest_rect.y, ow, oh), 
      src_bitmap, src_rect )
  end
  #--------------------------------------------------------------------------
  # * Draw Animated Sprite
  #--------------------------------------------------------------------------
  def draw_anim_sprite(x, y, w, h, name, hue, stance = 0)
    # Gets Frame
    frame = (Graphics.frame_count / Anim_Sprite_Reset_Frames) % 
            Anim_Sprite_Frame_Width
    # Draw Sprite
    draw_sprite(x, y, w, h, name, hue, stance, frame)
  end
  #--------------------------------------------------------------------------
  # * Draw Sprite
  #--------------------------------------------------------------------------
  def draw_sprite(x, y, w, h, name, hue, stance = 0, frame = 0)
    # Gets Bitmap
    bitmap = RPG::Cache.character(name, hue)
    # Bitmap Division
    cw = bitmap.width / Anim_Sprite_Frame_Width
    ch = bitmap.height / Anim_Sprite_Frame_Height
    # Gets Animation Offsets
    x_off, y_off = cw * frame, ch * stance
    # Clears Area
    self.fill_rect(Rect.new(x, y, w, h), Color.new(0, 0, 0, 0))
    # Draws Bitmap
    self.scale_blt(Rect.new(x, y, w, h), bitmap, 
      Rect.new(x_off, y_off, cw, ch))
  end

To use it, in your refresh method or main drawing phase, use

self.contents.draw_anim_sprite(x, y, w, h, name, hue)

It scales whatever character file automatically into your x, y, w, h rectangle and draws it depending on the frame count and Anim_Sprite_Reset_Frames constant.

Then, in your update method, you will need something like:
Code:
  def update
    # ...
    if Graphics.frame_count % Bitmap::Anim_Sprite_Reset_Frames == 0
      <draw_animted_sprite>
    end
    # ...
  end

It clears the area of where you are scaling the character sprite, and redraws it every reset frame.
 
Still Not working .... >_> I think I'm doing something wrong....


It's the Image It isn't showing up....

EDIT;::: Okay you see this is the Graphic:

http://img400.imageshack.us/img400/8128/lockerzb8.png[/IMG]

With this Script:

Code:
bitmap = @bitmap
    cw = bitmap.width / 4
    ch = bitmap.height
    @face_frame = 0
    #src_rect = Rect.new(@face_frame * 88, 0, 88, bitmap.height)
    #src_rect = Rect.new(0, 0, cw, ch)
    src_rect = Rect.new(0, 0, 0, 0)
    if @face_frame <= 3
      src_rect = nil
      @face_frame += 1
      src_rect = Rect.new(cw * @face_frame, 0, cw, ch)
    else
      @face_frame = 0
    end
    self.contents.blt(x - cw * 1.5, y - ch, bitmap, src_rect)

The Graphic Stops at Frame 2


So basically It just stops making frames



EDIT:::::: Got it working!!!!! yay!! ^_^
 
Status
Not open for further replies.

Thank you for viewing

HBGames is a leading amateur video game development forum and Discord server open to all ability levels. Feel free to have a nosey around!

Discord

Join our growing and active Discord server to discuss all aspects of game making in a relaxed environment. Join Us

Content

  • Our Games
  • Games in Development
  • Emoji by Twemoji.
    Top