I’m doing some animation script in RPG Maker XP (made with ruby) that allow you to display moving images. My question here is not strictly about RPG Maker, but in general term. This is the code I found out so far and it works, but with problem :
class Poser
attr_accessor :images
def initialize
@images = Sprite.new
@images.bitmap = RPG::Cache.picture('Character.png') #display picture
@images.x = 540 #place it on the bottom right corner of the screen
@images.y = 180
end
def move(x,y)
@images.x += x
@images.y += y
end
def animate(x,y,step,delay) #Animate moving the picture up and down with delay
forward = true
2.times { #the first loop, do code 2 times of :
step.times {
wait(delay) #wait x frame
if forward
move(x/step,y/step) #move the picture down
else
move(-x/step,-y/step) #move the picture up
end
}
wait(delay*3)
forward = false
}
end
def wait(time)
while time > 0
time -= 1
Graphics.update
end
end
end
Then I create an instance of it and called the method :
$test = Poser.new
$test.animate(0,10,10,10)
What the above code do is to move the picture up and down (just like breathing animation, your head bob up and down)
As you can see, Im using loop functions to move the picture with delay. What I got is, I cannot do anything else until the animation finished. What I meant by “anything else” is such as walking around with my character, talk to NPC, I want to do those things while there is animation being played in the background. In the end, the game “paused” in the loop block.
Is there is another way to do animation without looping, or, anything that doesn’t “pause” the screen until animation is finished ? Thanks in advance.
Usually, games use a system called a game loop. A game loop is a loop in the main function of the program that executes as fast as it can. Each time it executes, it executes two functions (or these can be the body of the loop, that’s a design choice that you can make):
drawandupdate(update, thendraw).update‘s job is to change the positions of characters, usually by a formula ofposition=(x+pixelsPerSecond*secondsSinceLastTick,y+pixelsPerSecond*secondsSinceLastTick)(in game terminology, a tick is an iteration of the game loop). The system of a game loop is optimized for games, because every tick you can execute one tiny piece of each animation, fast enough together that they give an illusion of concurrency (game loops run many times per second).After
updatehas changed the position of entire objects (eg. a fast NPC is now 5 pixels further to the left),drawis used to draw the scene.drawcan do a couple of things. It can draw sprites at the locations indicated byupdate, and it can also maintain it’s own animations (small things like the animation of legs walking —updateshouldn’t set the position of the legs in the walking animation, just the new position of the character for this tick).I’m not sure if I am exactly answering your question, as I know nothing about RPG Maker (so maybe you have to do something else completely), but because you said in general, this is what it is.