Game apps for smartphones and tablets |
|
Research project PHBern |
With touch events, programs that react to touching the smartphone touchscreen can be created. In order to do so, we need to implement a GGTouchListener that is registered with addTouchListener() in GameGrid . Thereby, the events that you are interested in are indicated via an "QR" mask. By doing this, touch events that are not needed in the application are filtered in an elegant way. The following actions are implemented: click, doubleClick, drag, longPress, press and release. With the parameter class GGMouse, important information about the event can be collected, expecially about the current touch position. Example 1: With each touch of the touchscreen, a new clown fish is created. This is a nice example for object oriented programming. Each new clownfish is created as an instance of the class Clownfish. Since the movement of the fishes is implemented in the class Clownfish a new moving fish is created with each next mouse click. The TouchListener and Touchevent are implemented in the application class. In this example, you only need GGTouch.click.
| ![]() |
// AndroidEx15.java Create objects by clicking package app.ex15; import ch.aplu.android.*; public class AndroidEx15 extends GameGrid implements GGTouchListener { public AndroidEx15() { super(8, 8, cellZoom(62), RED, "reef", false); } public void main() { addTouchListener(this, GGTouch.click); doRun(); } public boolean touchEvent(GGTouch touch) { addActor(new Clownfish(), getTouchLocation()); return true; } } class Clownfish extends Actor { public Clownfish() { super("nemo"); } public void act() { if (getX() == 0 || getX() == 7) { turn(180); setHorzMirror(!isHorzMirror()); } move(); } } |
addTouchListener(this, GGTouch.click) | Registers the TouchListener, whereby only the positions of contact of the touchscreens are needed |
Location location = toLocationInGrid(touch.getX(), touch.getY()) | Saves the coordinate of the touching variables location |
addActor(new Clownfish(), location) | Creates a new clown fish at the position of contact |
Example 2: A mouse actor can be moved to a random position in the grid. In a switch structure, it is determined what should happen when it is pressed (press), released (release) or moved (drag).
| ![]() |
// AndroidEx16.java package app.ex16; import ch.aplu.android.*; public class AndroidEx16 extends GameGrid implements GGTouchListener { private Actor actor; public AndroidEx16() { super(8, 8, 0, RED); } public void main() { addActor(new Actor("mouse"), new Location(5, 5)); addTouchListener(this, GGTouch.press | GGTouch.drag | GGTouch.release); } public boolean touchEvent(GGTouch touch) { Location location = toLocationInGrid(touch.getX(), touch.getY()); switch (touch.getEvent()) { case GGTouch.press: actor = getOneActorAt(location); break; case GGTouch.drag: if (actor != null) actor.setLocation(location); break; case GGTouch.release: if (actor != null) { actor.show(0); actor.setLocation(location); } break; } refresh(); return true; } } |
Explanation to the program code:
addTouchListener(this, GGTouch.press | GGTouch.drag | GGTouch.release); | The touch listener is registered. Since the application class implements a GGTouchListener, a reference is handed over to it (this). Press, drag und release are constants of the class GGMouse that are connected with OR in order to determine the desired mouse events. |
Location location = toLocationInGrid(mouse.getX(), mouse.getY()) | Gives back the grid coordinates at the touch position, whereby only positions within the visible sphere can be considered |
actor = getOneActorAt(location) | Gives back the actor that is in the cell of contact |