The board has six specific kinds of events which are covered in the subsections below. All but one of these events are triggered when the user touches the screen and taps, swipes or drags a finger across the board.
9.5.1 Board touching actions
In addition to events, the Board datatype provides five methods which provide information about how the screen has been touched. These methods are listed in Table 9-11. However the tap, swipe and drag events explained later in this section are easier to program and their use is recommended.
Table 9-11 Touch methods of the Board datatype
9.5.2 gameloop event
The gameloop event contains code that needs to be run regularly and frequently. The event is triggered about every 50ms. It is a natural location to contain collision detection code, or to monitor the passage of time.
The gameloop event code should be efficient. If it takes too long to execute, the display may stutter and collisions may go undetected.
9.5.3 tap board event
The tap board event fires if there is tap anywhere on the board except on a position where a sprite is located. Tapping means that one’s finger leaves the screen at approximately the same position as where it first touched. Otherwise, the software will report a swipe event instead. The event fires once the finger is lifted.
As an example, the code below creates a new ball wherever there is a tap
the board.
event
tap board: board(x,y)
var
sprite := ◳board → create ellipse(10,10)
sprite → set pos(x,y)
The tap board event has two parameters x and y which give the position where the tap occurred.
9.5.4 swipe board event
The swipe board event is similar, except that the event code is passed four parameters. The first two show where the swipe started and the second two show the extent of the swipe in the x and y dimensions.
As an example, the code below creates a new sprite and gives it an initial speed which corresponds to the extent of the swipe.
event
swipe board: board(x, y, delta x, delta y)
var
sprite := ◳board → create ellipse(10,10)
sprite → set pos(x,y)
sprite → set speed(delta x, delta y)
9.5.5 tap sprite in XXX event
Tap events can be provided for sprites held in different sprite collections. This makes it easier to program one kind of action for a spaceship, say, and a different kind of action when tapping an asteroid, say.
If, for example, there is a global data variable named spaceships with the type Sprite Set, then an event named tap sprite in asteroids can be provided. The event is passed four parameters. These are the sprite which was tapped, the index of the sprite in the sprite set, and the coordinates of the sprite on the board.
A sample of code which uses the event is as follows:
action
main( )
…
◳asteroids := ◳board → create sprite set
// populate the board with spaceships and asteroids
…
event
tap sprite
in
asteroids(sprite, index, x, y)
// change the asteroid’s color
sprite → set color(colors → red)
9.5.6 swipe sprite in XXX event
The swipe sprite event is similar to the tap sprite event, except that one’s finger is swiped across the screen and the extent of the swipe is passed as two additional parameters. For example, the following code will cause the sprite which is swiped to start moving in the direction of the swipe.
event
swipe sprite
in
asteroids(sprite, index, x, y, delta x, delta y)
sprite → set speed(delta x, delta y)
9.5.7 drag sprite in XXX event
A drag event does not wait for the finger to be lifted from the screen, as with the tap and swipe events. It fires while one’s finger is still on the screen. It will repeatedly fire while the finger is in motion across the screen. The event is passed very similar parameters to the swipe sprite event, except that the last two parameters provide the extent of the dragging motion (so far).
The event can be used to temporarily set the speed of a dragged sprite to 0 and to display it at the current drag position. In this way, it will appear that the sprite is being held at the finger’s position.
Here is example coding with the asteroids:
event
drag sprite
in
asteroids(sprite, index, x, y, delta x, delta y)
sprite → set speed(0, 0)
sprite → set pos(x, y)
When the finger is lifted at the end of the motion, a swipe sprite event is triggered (if event code for that action has been provided).
9.5.8 tap sprite SSS, swipe sprite SSS, drag sprite SSS
Instead of having events associated with sprite sets, it is possible to have events associated with an individual sprite. To do this, the sprite must be promoted to be a global data variable (in the data section of a script).
If the data variable is named SSS, then the corresponding event names are tap sprite SSS, swipe sprite SSS and drag sprite SSS.