Events
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.
> Triggers action each time keyboard is pressed.
> onKey is the function that handles keyboard event(actions).
> 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
> 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
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:
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
Mouse Click Event
object.onClick = () ->
monkey.onClick = () ->
Using all three events together:
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. |