Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Animated Sprites
#1
This is a locked, single-post thread from Creation Asylum. Archived here to prevent its loss.
No support is given. If you are the owner of the thread, please contact administration.


Animated Sprites by Slipknot

This is a easy way to use controlled animated sprites, when you make a new animation, it is stored in a module called "Animation".
Code:
object = Animated_Sprite.new(bitmaps, delay, type)

Why?, because you can have 10 (or more) animations in a scene, but you only need update the module, and all the animation will be updated in the same time.
Code:
Animation.update

And instead of dispose all the animations, you only need to call:
Code:
Animation.clear
And all the animations will be disposed and deleted. A good moment for call it, is below "Graphics.freeze", when the Scene will change.

You can also freeze an animation, if you make it, the freezed animation will not be updated, and the frames will no change. You can also freeze all the animations:
Code:
# one animation
@anim.freeze
# all
Animation.freeze
And for return to their normal states use:
Code:
@anim.defreeze
# or
Animation.defreeze


How make a new one?

Easy, is like call a new Sprite, but with some arguments:
Code:
@anim1 = Animated_Sprite.new ["1", "2"], 20, "battler"

The bitmaps, are an array of names (strings) of each frame. Then the delay time is an integer (less of 10 is too fast), and the final part is the type of the animation, can be "picture", "icon", "character", "animation", "windowskin", ...

The Script Wrote:You only need, to paste this before Main:
Code:
#============================================================
#  ¦ Animated Sprites ¦
#------------------------------------------------------------------------------
#   by Slipknot
#============================================================

class Animated_Sprite < Sprite
  #--------------------------------------------------------------------------
  # Attributes
  #--------------------------------------------------------------------------
  attr_accessor :frame, :delay
  attr_reader :bitmaps, :freeze
  #--------------------------------------------------------------------------
  #  bitmaps: are the names of the bitmaps used for each frame
  #  delay: time between frame changes, 10 = 1 sec
  #  type: icon, picture, character, battler, ...
  #--------------------------------------------------------------------------
  def initialize bitmaps, delay=10, type="picture"
    super()
    @method = RPG::Cache.method type
    self.bitmap = @method.call bitmaps[0]
    @bitmaps = bitmaps
    @delay = delay
    @frame = @count = 0
    @freeze = false
    Animation.push self
  end
  #--------------------------------------------------------------------------
  # Self dispose and delete
  #--------------------------------------------------------------------------
  def dispose
    Animation.delete self
    super
    bitmap.dispose
  end
  #--------------------------------------------------------------------------
  # Hides and freeze, or show and defreeze
  #--------------------------------------------------------------------------
  def visible=(val)
    super(val)
    @freeze = (not val)
  end
  #--------------------------------------------------------------------------
  # Defreeze, play animation
  #--------------------------------------------------------------------------
  def defreeze
    @freeze = false
  end
  #--------------------------------------------------------------------------
  # Freeze, stop animation
  #--------------------------------------------------------------------------
  def freeze
    @freeze = true
  end
  #--------------------------------------------------------------------------
  # Return the number of frames
  #--------------------------------------------------------------------------
  def size
    @bitmaps.size
  end
  #--------------------------------------------------------------------------
  # Frame update
  #--------------------------------------------------------------------------
  def update
    return if @freeze or bitmaps.empty? or bitmaps.size == 1
    @count = (@count+1) % @delay
    return unless @count == 0
    @frame = (@frame+1) % bitmaps.size
    self.bitmap = @method.call bitmaps[@frame]
  end
end

module Animation
  #--------------------------------------------------------------------------
  # Data initialization
  #--------------------------------------------------------------------------
  @data = []
  #--------------------------------------------------------------------------
  # Update all animations
  #--------------------------------------------------------------------------
  def self.update
    return if @data.empty?
    @data.each do |o|
      o.update
    end
  end
  #--------------------------------------------------------------------------
  # Delete an animation
  #--------------------------------------------------------------------------
  def self.delete object
    @data.delete object
  end
  #--------------------------------------------------------------------------
  # Delete all the animations
  #--------------------------------------------------------------------------
  def self.clear
    return if @data.empty?
    @data.each do |o|
      o.dispose if not o.disposed?
    end
    @data.clear
  end
  #--------------------------------------------------------------------------
  # Freeze all the animations
  #--------------------------------------------------------------------------
  def self.freeze
    return if @data.empty?
    @data.each do |o|
      o.freeze
    end
  end
  #--------------------------------------------------------------------------
  # Defreeze all the animations
  #--------------------------------------------------------------------------
  def self.defreeze
    return if @data.empty?
    @data.each do |o|
      o.defreeze
    end
  end
  #--------------------------------------------------------------------------
  # Adds an animation
  #--------------------------------------------------------------------------
  def self.push object
    @data.push object
  end
end
}


Possibly Related Threads…
Thread Author Replies Views Last Post
  Animated Icons Samo the thief 0 2,413 12-11-2006, 01:00 PM
Last Post: Samo the thief
  Change menu character sprites to Pictures like in rpg maker 2003 Abyssos 0 3,628 09-28-2006, 01:00 PM
Last Post: Abyssos
  Animated Message Script dragonslayer 0 2,136 05-04-2005, 01:00 PM
Last Post: dragonslayer



Users browsing this thread: