Blorb Development Blog

Weblog for upcoming Android game in development, "Blorb"; developed by James Daniello.

Blorb is a top-down box-pusher puzzle game. Features include: multiple 'characters' (in way of a hologram of the main character), over 60+ compelling and challenging levels, and a modestly inexpensive price.

Jan 22

Simple onTouch press and release code for Android (Java)

I couldn’t find anywhere on the web a simple way to tell when the user was not touching the screen, so I figured I’d share my research. If you’re looking for a way to poll user touch screen input, or tell when the user releases or moves a touch, look no further:

Put this code in your View class where gameEngine is where you want the input sent:

@Override
public boolean onTouchEvent(MotionEvent event)
{ 
gameEngine.OnTouch(event);
return true;
}


Then in your game engine code do something like this:

public static void HandleTouch(MotionEvent e)
 {
     int eventaction = e.getAction(); 
      switch (eventaction ) { 
            case MotionEvent.ACTION_DOWN:{ // touch on the screen event
                int x = (int)e.getX();
                int y = (int)e.getY();
                if(buttonUp.checkWithin(x, y)){
                    touchPressedUP=true;
                    break;
                }else if 
                
                ...

                }
            }
            case MotionEvent.ACTION_MOVE:{ // move event
               int x = (int)e.getX();
               int y = (int)e.getY();
               if(buttonUp.checkWithin(x, y)){
                    touchPressedUP=true;
               }
               if(buttonDown.checkWithin(x, y)){ 
               
               ...
               
               }
               break;
            }
            case MotionEvent.ACTION_UP:{  // finger up event
                touchPressedUP=touchPressedDOWN=touchPressedLEFT=touchPressedRIGHT=touchPressedACTION=false;
                break;
            }
          }
 }


Now you can tell when the user is no longer pressing on the screen! There is no “onRelease” method or “noTouch” function which seems to make sense seeing as there is an “onTouch” method. Let me know if this works out for you!

James


Page 1 of 1