Responding to touch interaction
People have become used to interacting with a device by tapping and swiping on a touchscreen. In this recipe, we'll take a look at how you can detect when a user is touching the screen, and let your sketch respond to that interaction. We'll make a simple drawing application to see how it's done.
How to do it...
We'll start by writing the setup()
function and declare a boolean variable. If the value of this variable is true, we'll draw some things on the screen.
boolean touching = false; void setup() { size( displayWidth, displayHeight ); smooth(); background( 0 ); }
To respond to a user touching the screen, we'll override the surfaceTouchevent()
method. If we detect a touch we'll set the touching
variable to true
:
public boolean surfaceTouchEvent( MotionEvent event ) { if ( event.getAction() == 2 ) { touching = true; } else { touching = false; } return super.surfaceTouchEvent( event ); }
In the draw()
function, we'll add the mouseX...