1 of 9

Events

  • Actions triggered (caused) by users while the code is running.
  • Actions are triggered either by clicking on mouse, moving mouse or pressing a key on the keyboard.

  • The three events in Coding Adventure are :

1. Keyboard Event: Users press a key on keyboard

2. Mouse Click Event: Users click on the mouse

3. Mouse Move Event: Users move the mouse cursor.

2 of 9

  1. Keyboard Event

> Triggers action each time keyboard is pressed.

> onKey is the function that handles keyboard event(actions).

  • onKey function is called each time a key is pressed on the keyboard.
  • onKey is the event handler for keyboard event.

> onKey function is used with ‘if statement’ with the parameter “key” as in the following example:

onKey=(key)->

………if key==‘w’

……………step 3

………if key== ‘d’

…………….turn right

3 of 9

> The onKey function recieves an argument which is the key being pressed on the keyboard (It means that the value of ‘key’ is the key that is pressed on the keyboard)

What is keyboard event used for?

Keyboard event is used:

>To move the characters in the game

  • To open the gate by pressing on the keys that appear on the gate

4 of 9

Mouse Move Event

> Triggers action each time user moves the mouse.

> The function that handles Mouse Move Event is onMouseMove

> onMouseMove is the Mouse Move Event Handler.

> onMouseMove function is called each time the mouse moves.

> pos is a parameter that receives an argument which is the position of mouse cursor.

> The position consists of two coordinates:

      • pos.x - the x position of the cursor
      • pos.y - the y position of the cursor

5 of 9

Syntax to use onMouseMove:

onMouseMove=(pos)->

…….turnTo pos

> The functions setX and setY are used to set the bat’s x and y positions respectively.

> When we call setX and setY functions we need to write as bat.setX and bat.setY.

onMouseMove=(pos)->

…….bat.setY pos.y

…… bat.setX pos.x

6 of 9

Mouse Click Event

  • Triggers an action each time the user clicks the object
  • The function that handles the Mouse Click event is onClick.
  • onClick is the Mouse Click Event Handler.
  • onClick function does not have any parameter.
  • onClick is called each time mouse is clicked.

7 of 9

  • We can define the onClick function for all the characters and objects on our screen.

  • The syntax of the onClick function:

object.onClick = () ->

monkey.onClick = () ->

8 of 9

Using all three events together:

  • onKey - to step

  • onMouseMove - to turn to the position of the cursor

  • onClick - to choose which animal will move/turn

9 of 9

onClick

grab(), sleeping()

- User defined function

-Users need to define the function

-It is called each time user clicks the mouse (Needs user interaction)

Eg: The monkey grabs the banana only when the user clicks the mouse.

-Predefined (built-in) function

-Users need not define the function

-It is automatically called in the Script without user interaction

Eg: The monkey grabs the banana automatically when the code is run.