Game apps for smartphones and tablets

Research project PHBern  
HomeStart online editorAndroid TurtlegraphicsPrintJava-Online

Animated actors


In many games, characters are not just moved around, but they also have to change their appearance with the movements. The user should have the impression that characters eat, run, swim and so on. In order to realize that in a simple way, several sprites can be assigned to one actor, of which only one is visible at a time. The number of sprites are adressed over a consecutive number idSprite (show(0), show(1), ...). By using the method showNextSprite() the images are displayed in sequence and then, image 0 is displayed again. When the actor is is turnable, the sprite images are turned rotating with them automatically.

Example 1: Two sprites are assigned to the actor pacman that are visible in turns.  
 

Run this example (first connect smartphone or start emulator)

Edit this example in the Online Editor

App installieren auf Smartphone oder Tablet

Download sources (AndroidEx8.zip)


// AndroidEx8.java

package app.ex8;

import ch.aplu.android.*;

public class AndroidEx8 extends GameGrid 
{
  public AndroidEx8()
  {
    super(8, 8, 0, RED);
  }

  public void main()
  {
    Pacman paki = new Pacman();
    addActor(paki, new Location(0, 0));  
    doRun();
  }
}

class Pacman extends Actor
{
  public Pacman()
  {
    super(true"pacman"2);
  }

  public void act()
  {
    move();
    if (!isMoveValid())
      turn(90);
    showNextSprite();
  }
}

Explanations to the program code:
super(true, "packman", 2); The number of sprites is displayed when the actor is created as parameter. In our example, two images, pacman_0.png and pacman_1.png are used.
move();
if (!isMoveValid())
   turn(90);

The pacman moves along the edge

showNextSprite()

The images pacman_0.png and pacman_1.png are displayed in turns.