Animations are one of the most important game items, don't you think?
The JPlay has a specific class to handle the animations.
This class is called Animation. Isn't this a suggestive name?
1 - How to create an animation?
To create an animation we need one image which has to contain some frames. The number of frames is your choice.
A frame is a piece of the image, responsible for the movement of the animation.
Example: we have the image below:
Note that there are four drawings of the Megaman in a single
image. Such that each one of these drawings can be called a frame.
Therefore, this image has four frames.
The concept of frame is very important in animation, keep it in mind.
2 - Instantiating the class Animation.
The Animation class has three constructors.
//The name of the image that will be used is passed and it will have only one frame.
Animation(String fileName);
//The name of the image that will be used and the number of the frames of this image are passed.
Animation(String fileName, int numberFrames);
//The name of the image that will be used and the number of the frames are passed, and the last
//parameter serves to say whether the animation will loop or not.
Animation(String fileName, int numberFrames, boolean loop);
To create an object of this class, we proceed as follows:
String imageName = "animation01.png";
int numberofFrames = 4;
Animation animation = new Animation(imageName, numberofFrames);
3 - Setting the time between the change of frames.
In animation, frames must change after a certain time. To
inform the time between the change of frames or the time in which each frame
will be displayed on the screen, we use the method:
public void setTotalDuration(long time);
Example:
int numberofFrames = 4;
int totalTime = 1000; //Time in milliseconds.
String imageName = "robot.png";
Animation animation = Animation(imageName, numberofFrames);
animation.setTotalDuration(totalTime);
If we have 4 frames and the total time is 1000 milliseconds, then each
frame will be displayed on the screen during 1000 / 4 = 250 milliseconds,
Note: Remember that 1 second equals to 1000 milliseconds, 1s =
1000ms and that is always necessary to set the total duration of the animation.
4 - Running the animation.
To make the animation run, we use the method below that is responsible for the change of frames:
public void update();
Up until now we have:
Animation animation = new Animation( "animation01.png ", 4);
animation.setTotalDuration(125);
animation.update();
We are ready to create our first animation.
Example: Running an animation.
package animation001;
import jplay.Animation;
import jplay.GameImage;
import jplay.Window;
/**
* @author Gefersom Cardoso Lima
* Federal Fluminense University - UFF - Brazil
* Computer Science
*/
public class Animation001 {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Window window = new Window(800,600);
GameImage background = new GameImage("background.png");
Animation animation = new Animation("animation.png", 4);
animation.setTotalDuration(500);
while(true)
{
background.draw();
animation.draw();
window.update();
animation.update();
}
}
}
5 - Animating a subset of frames
To animate a subset of frames we can use the methods:
//The initial and final frame numbers must be passed as parameters.
void setSequence(int initialFrame, int finalFrame);
//In this one we have the freedom to choose if the chosen subset will be executed in loop.
void setSequence(int initialFrame, int finalFrame, booleanloop);
//In this one we can choose the total execution time of the subset.
void setSequenceTime(int initialFrame, int finalFrame, long time);
//This method makes the junction of the second and the third methods mentioned above.
void setSequenceTime(int initialFrame, int finalFrame, boolean loop, long time);
If we had an image with 28 frames and execute
animation.setSequence(0,13), the frames to be used in the animation
will be 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13.
However if we execute animation.setSequence(14, 27) the frames to be
used in the animation will be 14, 15, 16, 17, 18, 19, 20, 21, 22, 23,
25, 25, 26, 27.
Example: running a subset of frames.
package animation002;
import jplay.Animation;
import jplay.GameImage;
import jplay.Window;
/**
* @author Gefersom Cardoso Lima
* Federal Fluminense University - UFF - Brazil
* Computer Science
*/
public class Animation002 {
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Window window = new Window(800,600);
GameImage background = new GameImage("background.png");
GameImage megaMan = new GameImage("minorAnimation.png");
Animation animation = new Animation("animation.png", 28);
animation.setTotalDuration(2000);
animation.setSequence(0, 13);
animation.y = 250;
animation.x = 200;
megaMan.y = 150;
while(true)
{
background.draw();
megaMan.draw();
animation.draw();
window.update();
animation.update();
}
}
}
Example: Accelerate or decelerate the execution of the frames.
package Animation003;
import jplay.Animation;
import jplay.GameImage;
import jplay.Keyboard;
import jplay.Window;
import java.awt.Color;
/**
* @author Gefersom Cardoso Lima
* Federal Fluminense University - UFF - Brazil
* Computer Science
*/
//Accelerate or decelerate a frameset.
public class Animation003
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Window window = new Window(800,600);
GameImage fundo = new GameImage("background.png");
Animation animacao = new Animation("inuyasha.png", 13);
Keyboard keyboard = window.getKeyboard();
animation.setTotalDuration(1000000);
animation.y = 250;
animation.x = 200;
while(true)
{
background.draw();
animation.draw();
window.drawText("Press Space Bar to accelerate or Enter to decelerate.", 200, 210, Color.yellow);
window.update();
if (keyboard.keyDown(Keyboard.ENTER_KEY))
{
animation.setSequenceTime(0, 12, 6000);
}
else
{
if (keyboard.keyDown(Keyboard.SPACE_KEY))
animation.setSequenceTime(0, 12, 1500);
}
animation.update();
}
}
}
6 - Changing the frames manually.
To set which frame to be draw we use the method:
//Sets the frame that will be draw in the next loop.
public void setCurrFrame(int frame);
Note: If you are using this method it is not necessary to set the duration of the animation.
Example: Changing the frames manually.
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package Animation004;
import jplay.Animation;
import jplay.GameImage;
import jplay.Keyboard;
import jplay.Window;
/**
* @author Gefersom Cardoso Lima
* Federal Fluminense University - UFF - Brazil
* Computer Science
*/
//It sets the frame to be drawn.
public class Animation004
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Window window = new Window(800,600);
GameImage background = new GameImage("background.png");
Animation animation = new Animation("ship.png", 2);
Keyboard keyboard = window.getKeyboard();
animation.y = 250;
animation.x = 200;
while(true)
{
background.draw();
animation.draw();
window.update();
if (keyboard.keyDown(Keyboard.LEFT_KEY))
animation.setCurrFrame(0);
else
{
if (keyboard.keyDown(Keyboard.RIGHT_KEY))
animation.setCurrFrame(1);
}
}
}
}
7 - Setting a time for each frame.
In some animations certain part of the animation must be executed
faster or slower than other part. For this, there is the
following method:
public void setDuration(int frame, long time);
The parameters are: the number of the frame and the value of the duration time in which the frame must be shown.
Example: Frames with different change times.
package Animation005;
import JPlay.Animation;
import JPlay.GameImage;
import JPlay.Keyboard;
import JPlay.Window;
/**
* @author Gefersom Cardoso Lima
* Federal Fluminense University - UFF - Brazil
* Computer Science
*/
//Frames have different times of change
public class Animation005
{
/**
* @param args the command line arguments
*/
public static void main(String[] args)
{
Window window = new Window(800,600);
GameImage background = new GameImage("landscape.png");
Animation animation = new Animation("photos.png", 8);
animation.setDuration(0, 2000); // Frame 0 will shown for 2000 milliseconds.
animation.setDuration(1, 1800); // Frame 1 will shown for 1800 milliseconds.
animation.setDuration(2, 1600); // Frame 2 will shown for 1600 milliseconds.
animation.setDuration(3, 1400); // Frame 3 will shown for 1400 milliseconds.
animation.setDuration(4, 1200); // Frame 4 will shown for 1200 milliseconds.
animation.setDuration(5, 1000); // Frame 5 will shown for 1000 milliseconds.
animation.setDuration(6, 800); // Frame 6 will shown for 800 milliseconds.
animation.setDuration(7, 600); // Frame 7 will shown for 600 milliseconds.
animation.loop(false);
while(true)
{
background.draw();
animation.draw();
window.update();
animation.update();
}
}
}
8 - Hiding the animation.
//Stops drawing the image on the computer screen.
public void hide();
//Redraws the image on the computer screen.
public void unhide();
Note: Even if the animation is not shown on the screen, if the update() method
is being called, the frames will continue to be changed.
9 - Other methods.
//Returns true if the animation is not paused or passed by a stop, otherwise, returns false.
public boolean isPlaying();
//Returns the number of the current frame to be drawn.
public int getCurrFrame();
//Returns true if the animation will be repeated, otherwise, false.
public boolean isLooping();
//Returns the duration time in milliseconds in which the frame will be shown on the screen.
public long getDuration(int frame);
//Stops the execution of the animation and returns to the first frame.
public void stop();
//Pauses the execution of the animation.
public void pause();
//Sets the frame that will be used to start the animation.
public void setInitialFrame(int frame);
//Returns the number of the frame used to start the animation.
public int getInitalFrame();
//Sets the number of the frame that will be presented for final.
public void setFinalFrame(int frame);
//Returns the number of the final frame that is used in the animation.
public int getFinalFrame();
//Returns the total execution time of the set of chosen frames.
public long geTotalDuration();
//If you want the animation to be executed in loop, pass the value 'true',
//otherwise, pass the value 'false'.
public void loop(boolean value);
The Animation class has no methods that make the animation move across
the screen. For this you can use the public variables 'x' and 'y'.