01-29-2006, 01:00 PM
(This post was last modified: 07-25-2017, 04:30 AM by DerVVulfman.)
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.
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
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
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