Program a Touch Event

Add dots when you tap the screen.

Step 1

First we need to add some Touch event handlers. These are blocks of code that will tell the app to draw a dot when the user taps the screen.

In the Blocks editor, open the drawer for Canvas1 and drag the when Canvas1.Touched event handler block to the Viewer. 

Step 2

Open the drawer for Canvas1, drag out the call Canvas1.DrawCircle command block and snap it to the when Canvas1.Touched block. There are four slots in this command block:

  • centerX and centerY -  these say where the circle should be drawn
  • radius - the size of the circle
  • fill - “true” to draw a filled circle, or “false” to draw an outlined circle

Step 3

Hover the mouse over the when Canvas1.Touched block to pull out the get x and get y blocks that you need. This gives you the coordinates (x, y) where the screen is touched. 

  • Open the Math drawer
  • Drag out an empty number “0” block
  • Snap it to the radius slot 
  • Click on the “0”to change the radius to “5”
  • Set the fill to “true”

The code should look like this:

Step 4

Test the code!