General touch input

Problem

I want to listen to the screen getting touched and determine myself whether or not the user touches something that I care about or want things to happen no matter where the user touches the screen.

Solution

First, make sure that the game reads the Touch Screen. This is done by letting your BaseGameAcitvity implement IOnSceneTouchListener.

Then, on onLoadScene(), do scene.setOnSceneTouchListener(this);. As you register touch listener on whole scene then no matter where you touch onSceneTouchEvent() will be called.

Then, as a part of the interface IOnSceneTouchListener, you will have to implement an operation

public boolean onSceneTouchEvent(Scene pScene, TouchEvent pSceneTouchEvent) {
//your code here!
}

Then, you can get the x and y values of the touch with pSceneTouchEvent.getX() and .getY(). And if you call pSceneTouchEvent and it returns TouchEvent.ACTION_DOWN, you know for sure that the screen was touched right now. Similarly, TouchEvent.ACTION_UP is returned when the user lists their finger.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License