Are you looking for a fun, free coding activity that you can do with your class? CodeMonkey has you covered in our newest blog column – Offline Coding Activities. You don’t need any coding experience to carry-out this activity – simply read the following exercise and then do it tomorrow in class! This week’s 15 minute-long activity will help students understand how event-handlers work in programming.
The following activity facilitates the understanding of an event (when something in the game happens) and an event handler, specifically the click event (this is what happens when clicking on a sprite).
1. First, ask for four volunteers:
- Nominate one volunteer to be the “event’s causer”. Their role will be to trigger the event in the game.
- Nominate the other three volunteers to respond to an event (or act as the “event handlers” who carry out the event). Decide with the “event handlers” on their actions (for example, volunteer 2 will walk to the window, volunteer 3 will count until 10 in Spanish, volunteer 4 will write “hello” on the board, etc.)
2. Second, tell the “event handlers” that they can only execute their action when the “event causer” taps on their shoulders.
3. Ask the “event causer” to start going around and tap the “event handlers’” shoulders. Each “event handler” should perform his or her action only when the “event causer” taps their shoulder. Ask the “event causer” to also:
- tap on other students in the class ( who should not do anything). Ask your class why the students are not doing anything? This is because they were not programmed to execute an action.
- tap on other objects in the class (i.e. a chair or desk). Nothing will happen. Ask your students why is nothing happening? This is because these objects do not have the capability of being handled in an event.
Events are actions triggered by the user that take place while the code is running, and as a result affect what is happening.
Computer events are:
- Pressing a key on the keyboard
- Clicking on the mouse
- Moving the mouse
When we click on the mouse, we trigger the ‘mouse click event’ and the ‘onClick’ function is called. Such a function is also called an event handler. The onClick function has no parameters. Anytime the mouse is clicked on, the function onClicked is called. If the function is not defined in the sprite, then nothing will happen when the sprite is clicked on. When the mouse is clicked on anything other than a sprite, nothing will happen, because there is no object that listens (or “waits”) for the trigger.
This offline coding activity is from Lesson 2 in CodeMonkey’s Sprite Animations Course. For more fun activities and a detailed curriculum guide on how to teach CodeMonkey, sign-up for our free teacher trial here.