Game apps for smartphones and tablets |
|
Research project PHBern |
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.
|
Example 1: The actor nemo moves horizontally from cell to cell. If he is in a border cell, he turns around and swims back.
|
![]() |
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 |