Game apps for smartphones and tablets

Research project PHBern  
HomeStart online editorAndroid TurtlegraphicsPrintJava-Online

Actors

All figures and items that appear in the game window are considered to be be objects and are described with classes that are derived from the class Actor. By that, the OOP concept can be realized already with the first programming examples. A sprite image is attributed to every actor. The file name of the image is indicated in the constructor of the actor. (It is as well possible that several images are attributed to one actor).  
The main concept consists in giving a dynamic "life of their own" to the actors. As soon as "run" clicked, a simulation cycle is started in which the method act() is selected. The simulation cycle can be changed with the volume control of the smartphone. In each simulation cycle, the background is built up again. Therewith, animations can be realized in a simple way (this elegant concept war taken over from Greenfoot ).

 

Most smartphones have the operating buttons home, menu, und back. Together with the volume control, these buttons are used for navigation, analoge to the navigation bar with GameGrid.


The smartphone keys are allocated as follows:

Home: End of the program
Menu: Run, if the program has not started yet or after reset; if there is a log click after a break
Pause, if the program is already running
Step, if there is a short click after a pause
Back: Reset

Example 1: The actor nemo moves horizontally from cell to cell. If he is in a border cell, he turns around and swims back.

Run this example (first connect smartphone or emulator)

Edit this example in the Online Editor

App installieren auf Smartphone oder Tablet

Download sources (AndroidEx1.zip)

// AndroidEx1.java

package app.ex1;

import ch.aplu.android.*;

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

  public void main()
  {
    Fish nemo = new Fish();
    addActor(nemo, new Location(1, 1));
    doRun();
  }
}

class Fish extends Actor
{
  public Fish()
  {
    super("nemo");
  }

  public void act()
  {
    move();
  }
}

 

 

Explanations to the program code:

class Fish extends Actor The class fish is diverted from the class actor and therby inherits all (non private methods) of this class
public Fish()
{
    super("nemo");
}
In the constructor of the fish class, the file name of the corresponding sprite image is indicated
act() In the method, it is declared what the actor should do
move() Moves the actor into the neighbouring cell. When the actor is created, it receives the standad direction g 0° (direction east)
if (isNearBorder())
   turn(180);
If the Nemo is at the border, he turns his moving direction by 180°
Fish nemo = new Fish() Creates an object nemo of the fish class. Since fish is diverted from the class actor, Nemo is an actor.
addActor(nemo,  new Location (1 ,  1)) The actor is added to the GameGrid and appears at the indicated position. In a 10x10 grid, the values can be from 0 to 9. The actor image is positioned centered and can as well go beyond the cell borders.
doRun() When the method doRun() is activated (// delete), the simulation cycle is started automatically