Training and Consulting | ||
| Home |
| Previous Page - Refactoring the code | Current Page - 14 - Frames | Next Page - Frames - II |
| Return to the index of free Java tutorials |
FramesNow that we have a lot of critters moving everywhere, we'll try to add animation to them. In most 2D games, ranging from shooters to RPGs to strategy games, animations are almost synonymous with "frames". Instead of showing a fixed image, the actors cycle through a sequence of images called "frames". The more frames, the smoother the animation. In this case, our monsters will have only two frames, stored in "bicho0.gif" y "bicho1.gif":
Since frames is something that any entity in the game can have, we will add this functionality to the base Actor class. The property spriteName will disappear and will be substituted by spriteNames - an array of strings representing the names of the frames. Also, the act() method will be the one in charge to update the frame counter
1 /** 2 * Curso B?sico de desarrollo de Juegos en Java - Invaders 3 * 4 * (c) 2004 Planetalia S.L. - Todos los derechos reservados. Prohibida su reproducci?n 5 * 6 * http://www.planetalia.com 7 * 8 */ 9 package version14; 10 11 import java.awt.Graphics2D; 12 import java.awt.image.BufferedImage; 13 14 public class Actor { 15 protected int x,y; 16 protected int width, height; 17 protected String[] spriteNames; 18 protected int currentFrame; 19 protected Stage stage; 20 protected SpriteCache spriteCache; 21 22 public Actor(Stage stage) { 23 this.stage = stage; 24 spriteCache = stage.getSpriteCache(); 25 currentFrame = 0; 26 } 27 It's interesting to note the way in which the frame counter is incremented, using the % operator to limit its value to the range 0 .. spriteNames.length -1 Since now we have some code in the act() method of Actor, clases that extend from this one cannot simply override the method, but must also remember to call the original act() method before perforiming their specific actions. This means that we must allso change the Monster class as follows :/
1 /** 2 * Curso B?sico de desarrollo de Juegos en Java - Invaders 3 * 4 * (c) 2004 Planetalia S.L. - Todos los derechos reservados. Prohibida su reproducci?n 5 * 6 * http://www.planetalia.com 7 * 8 */ 9 package version14; 10 11 public class Monster extends Actor { 12 protected int vx; 13 14 public Monster(Stage stage) { 15 super(stage); 16 setSpriteNames( new String[] {"bicho0.gif","bicho1.gif"}); 17 } 18 19 public void act() { 21 x+=vx; 22 if (x < 0 || x > Stage.WIDTH) 23 vx = -vx; 24 } 25 26 public int getVx() { return vx; } 27 public void setVx(int i) {vx = i; }} 28 |
||||||||||||||
Do you want to be notified when new tutorials or lessons are published? Press here
| Actor.java | Invaders.java | Monster.java | SpriteCache.java |
| Stage.java |
| Previous Page - Refactoring the code | Current Page - 14 - Frames | Next Page - Frames - II |
| Return to the index of free Java tutorials |
(c) 2004 Planetalia S.L. All rights reserved. Unauthorized reproduction and/or mirroring is not permitted