Sprite Description
Sprite, in GTGE terminology, is used to describe every objects in game that has an image and has its own characteristic.
You could imagine a sprite as an image that located somewhere on the screen, whereas this image could be moving, animated, and has its own characteristic.
Sprite, in GTGE, is represented by Sprite
class that reside in com.golden.gamedev.object
package.
Sprite Initialization
Tutorial7_1.java [view online]Initialization of a sprite is as followed :
- Create new sprite object :
class :: Sprite Syntax: public Sprite(BufferedImage image, double x, double y); whereas : image = the sprite image x = the sprite x coordinate y = the sprite y coordinate For example: create a sprite with image "player.png" and located on 100, 200 in screen BufferedImage image = getImage("player.png"); double x = 100; double y = 200; Sprite hero = new Sprite(image, x, y);
- Updating the sprite :
class :: Sprite Syntax: public void update(long elapsedTime); whereas : elapsedTime = time elapsed since last update this value is taken from Game class
- Rendering the sprite into screen :
class :: Sprite Syntax: public void render(Graphics2D g); whereas : g = graphics object where the game is rendered this value is taken from Game class too
Example :
file :: YourGame.java // JFC import java.awt.*; import java.awt.image.*; // GTGE import com.golden.gamedev.*; import com.golden.gamedev.object.*; public class YourGame extends Game { Sprite hero; /****************************************************************************/ /**************************** GAME SKELETON *********************************/ /****************************************************************************/ public void initResources() { BufferedImage image = getImage("player.png"); double x = 100; double y = 200; hero = new Sprite(image, x, y); } public void update(long elapsedTime) { hero.update(elapsedTime); } public void render(Graphics2D g) { hero.render(g); } /****************************************************************************/ /***************************** START-POINT **********************************/ /****************************************************************************/ public static void main(String[] args) { GameLoader game = new GameLoader(); game.setup(new YourGame(), new Dimension(640,480), false); game.start(); } }
Using The Sprite
Set Sprite Location
To set sprite location into specified coordinate use :
class :: Sprite Syntax: public void setLocation(double xs, double ys); whereas : xs = the sprite x coordinate ys = the sprite y coordinate
Moving Sprite
There are 2 functions to move a sprite :
class :: Sprite Syntax: public void setSpeed(double vx, double vy); public void move(double dx, double dy); whereas : vx = the sprite speed toward x-axis vy = the sprite speed toward y-axis dx = sprite delta x movement dy = sprite delta y movementThe difference between
setSpeed(double vx, double vy)
and move(double dx, double dy)
is :setSpeed(double vx, double vy)
move sprite continously until you set the speed back to zero; whilemove(double dx, double dy)
move sprite instantly and stop.
Example :
file :: YourGame.java // JFC import java.awt.*; import java.awt.event.*; // GTGE import com.golden.gamedev.*; import com.golden.gamedev.object.*; public class YourGame extends Game { Sprite hero; /****************************************************************************/ /**************************** GAME SKELETON *********************************/ /****************************************************************************/ public void initResources() { hero = new Sprite(getImage("player.png"), 100, 200); hero.setLocation(50, 50); hero.setSpeed(0.1, 0); } public void update(long elapsedTime) { hero.update(elapsedTime); if (keyDown(KeyEvent.VK_DOWN)) { hero.move(0, 0.1*elapsedTime); } } public void render(Graphics2D g) { hero.render(g); } /****************************************************************************/ /***************************** START-POINT **********************************/ /****************************************************************************/ public static void main(String[] args) { GameLoader game = new GameLoader(); game.setup(new YourGame(), new Dimension(640,480), false); game.start(); } }
Types of Sprite
Tutorial7_2.java [view online]There are several types of sprites, one of them is AnimatedSprite
class. AnimatedSprite
is subclass of Sprite
class that able to animate. This kind of sprite need an array of images to make up the sprite animation. Use image engine to cut an image into a good animation.
AnimatedSprite
initialization is as followed :
class :: AnimatedSprite Syntax: public AnimatedSprite(BufferedImage[] image, double x, double y); whereas : image = an array of image for the sprite image x = sprite x coordinate y = sprite y coordinate For example: create a sprite with image "player.png" that cutted into 4 columns 2 rows BufferedImage[] images = getImages("player.png", 4, 2); double x = 100; double y = 200; AnimatedSprite hero = new AnimatedSprite(images, x, y);
Example :
file :: YourGame.java // JFC import java.awt.*; // GTGE import com.golden.gamedev.*; import com.golden.gamedev.object.*; public class YourGame extends Game { AnimatedSprite hero; public void initResources() { hero = new AnimatedSprite(getImages("player.png", 4, 2), 100, 200); hero.setAnimate(true); hero.setLoopAnim(true); } public void update(long elapsedTime) { hero.update(elapsedTime); } public void render(Graphics2D g) { hero.render(g); } public static void main(String[] args) { GameLoader game = new GameLoader(); game.setup(new YourGame(), new Dimension(640,480), false); game.start(); } }
Other Important Things
Tutorial7_3.java [view online]Timer.java [view online]
To make a game / sprite independent of game frame rate, always use Timer
class. Timer
class is a real time counter class.
The Timer
class initialization and how to use it :
class :: Timer Syntax: public Timer(int delay); public boolean action(long elapsedTime); whereas : delay = actual time in milliseconds (1 sec=1000 ms) elapsedTime = time elapsed since last update
Example: making a sprite to fire every one second :
file :: YourGame.java // JFC import java.awt.*; // GTGE import com.golden.gamedev.*; import com.golden.gamedev.object.*; public class YourGame extends Game { Sprite hero; Timer fireRate; public void initResources() { hero = new Sprite(getImage("player.png"), 100, 200); fireRate = new Timer(1000); } public void update(long elapsedTime) { hero.update(elapsedTime); if (fireRate.action(elapsedTime)) { // fire!! playSound("fire.wav"); } } public void render(Graphics2D g) { hero.render(g); } public static void main(String[] args) { GameLoader game = new GameLoader(); game.setup(new YourGame(), new Dimension(640,480), false); game.start(); } }
AnimatedSprite
also use Timer
class for controlling animation speed, that way the sprite animation is independent of game frame rate.