First, you didn't call
super in the
update method. That will result in your inputs being ignored (it will look like it freezes your game).
Either call a
super there somewhere, or call
Input.update.
What you did in that method will pause your entire screen for 10 frames, which is certainly not what you want.
You want to set the number of frames (in time) between each animation frame to 10 without affecting anything else on the scene.
To do this effectively, you need to make
image-sheets, not single images. Using an image sheet and just moving around the rect of the sprite is way better performance-wise than constantly clearing and drawing the bitmap (with
blt) on the sprite or constantly disposing and changing the bitmap.
Get familiar with image-sheets first. It must follow a format. The basic ones can simply go in a horizontal or vertical line, while some more complicated ones (code-wise, graphically it is almost the same, just some position changes) follow a grid-like sheet for positioning.
I usually just go with horizontal or vertical sheets, because I need the other axis for something else (well, most of the time), like direction based images and such. If you don't need the other axis for anything else, you might as well try the grid type sheet (but the code for that one is a bit more complicated).
When you made your image-sheet(s), you can start to work on your code. What you want to do is to make an entirely new class solely dedicated for maintaining animated images from an image-sheet. That way, you can re-use it for specific purposes, and you can also use it on any scene, anywhere and anytime.
The
initialize method should contain the parameters for that instance of the class in it's arguments. That way, you can initialize the same class with different starting values.
Well, it's the same as the arguments in your script call, but this time, the entire instance gets these values.
You can take a look at this class I made:
http://pastebin.com/t7QrwEH7
I added some notes and instructions there to explain what each method does.
Now you got a class which can animate image using image-sheets. How to use it?
Well, you just store an instance of the class in a variable and update it until you dispose it.
Making one is as simple as making a sprite (check the header of the script).
To show you an example, here is a sample scene:
Spoiler
class Scene_AnimExample < Scene_Base
def update
super
update_buttons
end
def update_buttons
trigger_anim_image if Input.trigger?🙂C)
trigger_animation if Input.trigger?🙂UP)
trigger_flash if Input.trigger?🙂DOWN)
return_scene if Input.trigger?🙂😎
@anim_img.update if @anim_img && !@anim_img.disposed?
end
def trigger_anim_image
Sound.play_ok
@anim_img.dispose if @anim_img && !@anim_img.disposed?
data = { # An example data setup. Edit them how you want!
:img => "hp2a_fill",
😮pa => 255, :z => 100,
:rows => 4, # Meaning it will be a vertical image-sheet!
:frames => [0,1,2,3,2,1],
:wait => 8,
}
folder = "Graphics/AnimGauges/HP/"
@anim_img = AnimatedImage.new(data,nil,folder)
@anim_img.x = Graphics.width/2
@anim_img.y = Graphics.height/2
end
def trigger_animation # Plays a random animation.
return unless @anim_img && !@anim_img.disposed?
@anim_img.set_anim🙂test1,rand(100)+1)
end
def trigger_flash # Plays a flash effect.
return unless @anim_img && !@anim_img.disposed?
r = rand(255); g = rand(255); b = rand(255); a = rand(255)
col = Color.new(r,g,b,a)
dur = rand(120) + 60
@anim_img.start_flash(col,dur)
end
def dispose_all_windows
@anim_img.dispose if @anim_img
super
end
end
Open the scene and press the confirm button. You will see your image appear and it should be animated (if your settings are correct).
You can press the cancel button to exit the scene.
You can press UP to play an animation on the image.
You can press DOWN to play a flash effect on the image.
Remember to change the data/folder settings in the
trigger_anim_image method to your own settings!
Again, this is just an example scene.
You can also make a container for these images in
Scene_Base itself, and do the update/dispose methods there directly. That way, you can initiate this class literally anywhere and it will work. From events, on custom scenes, and so on. This requires some proper initialize and disposal methods to function (so that the images show up on the scenes you specified, and not on all the scenes), so I won't go that way now.
In the case of your example, you need it on the map, right?
I will show you how to make a container on that scene for them. Look at the code below:
Spoiler
class Game_System
attr_accessor :map_anim_imgs
# This will store all the animated images on the map currently!
def map_anim_imgs
@map_anim_imgs = {} if @map_anim_imgs.nil?
return @map_anim_imgs
end
end
class Scene_Map < Scene_Base
alias add_anim_imgs9937 start
def start
add_anim_imgs9937
init_anim_images
end
# Initializing the images based on the container in Game_System!
def init_anim_images
@anim_imgs = {}
$game_system.map_anim_imgs.each do |key,data|
@anim_imgs[key] = AnimatedImage.new(data[:data],nil,data[:folder])
@anim_imgs[key].x = data[😛os][0]
@anim_imgs[key].y = data[😛os][1]
end
end
# This adds a new animated image on the map!
def add_anim_img(key,pos,data,folder)
@anim_imgs[key].dispose if @anim_imgs[key] && !@anim_imgs[key].disposed?
@anim_imgs[key] = AnimatedImage.new(data,nil,folder)
@anim_imgs[key].x = pos[0]
@anim_imgs[key].y = pos[1]
end
# This removes an animated image from the map.
def remove_anim_img(key)
@anim_imgs[key].dispose if @anim_imgs[key] && !@anim_imgs[key].disposed?
end
alias update_anim_imgs9222 update
def update
update_anim_imgs9222
update_anim_imgs
end
# Updating all animated images currently on the screen!
def update_anim_imgs
@anim_imgs.each_value {|img| next if img.nil? || img.disposed?; img.update }
end
def dispose_all_windows
dispose_anim_imgs
super
end
# Disposing all animated images currently on the screen!
def dispose_anim_imgs
@anim_imgs.each_value {|img| next if img.nil? || img.disposed?; img.dispose }
end
end
class Game_Interpreter
# Script call to show an animated image on the map!
# key = The unique key of the image. You can NOT use duplicates here!
# If you already have an image with the :test key, and still use the
# same key, the old image will be replaced with the new one!
# If you use a different key, you can show more than one images ta once!
# pos = The X and Y position of the image in an array. Sample: [x,y]
# data = The data of the image and it's animation settings.
# Uses the same format like explained in the script for the
# AnimatedImage class.
# folder = The folder where the image is.
# Optional. You can see it's default value.
def show_anim_img(key,pos,data,folder="Graphics/Pictures/")
$game_system.map_anim_imgs[key] = {😛os => pos, :data => data, :folder => folder}
SceneManager.scene.add_anim_img(key,pos,data,folder)
end
# Script call to remove an animated image from the map!
# key = The unique key of the image.
# You must use a valid key otherwise nothing will happen!
def remove_anim_img(key)
return unless $game_system.map_anim_imgs[key]
$game_system.map_anim_imgs.delete(key)
SceneManager.scene.remove_anim_img(key)
end
end
Now you can show and hide these images however you want on the map scene whenever you want.
Depending on what you want to do, you might need to change some things here and there.
You can also expand that
AnimatedImage class I made with your own methods if needed as well.
I wonder if I reached the character limit or not... Lets see...
😀