Planetalia - Java Training

Training and Consulting

Home

Tutorial - Writing a Space Invaders game in Java

  Previous Page - Refactoring the code Current Page - 14 - Frames   Next Page - Frames - II
  Return to the index of free Java tutorials  

Frames

(c) Alexander Hristov

Now 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":

Monstruo del curso de java Monstruo del curso de java

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      
28 public void paint(Graphics2D g){ 29 g.drawImage( spriteCache.getSprite(spriteNames[currentFrame]), x,y, stage ); 30 }
31 32 public int getX() { return x; } 33 public void setX(int i) { x = i; } 34 35 public int getY() { return y; } 36 public void setY(int i) { y = i; } 37 38 public void setSpriteNames(String[] names) { 39 spriteNames = names; 40 height = 0; 41 width = 0; 42 for (int i = 0; i < names.length; i++ ) { 43 BufferedImage image = spriteCache.getSprite(spriteNames[i]); 44 height = Math.max(height,image.getHeight()); 45 width = Math.max(width,image.getWidth()); 46 } 47 } 48 49 public int getHeight() { return height; } 50 public int getWidth() { return width; } 51 public void setHeight(int i) {height = i; } 52 public void setWidth(int i) { width = i; } 53
54 public void act() { 55 currentFrame = (currentFrame + 1) % spriteNames.length; 56 }
57 } 58

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() {
20 super.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


Full list of Java source files for this step

Actor.java Invaders.java Monster.java SpriteCache.java
Stage.java      

Full list of resources

bicho.gif bicho0.gif bicho1.gif bicho2.gif
bombD.gif bombDL.gif bombDR.gif bombL.gif
bombR.gif bombU.gif bombUL.gif bombUR.gif
disparo.gif disparo0.gif disparo1.gif disparo2.gif
explosion.wav misil.gif missile.wav musica.wav
nave.gif oceano.gif photon.wav test.gif
Thumbs.db      

  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