Events start actions — Computing, 4–6
A program can wait for something to happen, then begin an action. Computing, 4–6 years.
What is an event?
An event is something that happens and tells a program when to begin. Tapping a button, pressing a key or hearing a sound can start an action. The program waits for that signal instead of acting all the time.
Why wait for an event?
Without events, a game might move before you touch anything, and a doorbell might ring at the wrong time. Events connect an action to the thing that should cause it. This makes a program respond to you and to the world around it.
A button starts a sound
Imagine a simple music app. First, the program waits for the event “the blue button is tapped”. Next, it checks which sound belongs to that button. Finally, it plays a drum sound. If nobody taps the button, the drum stays quiet.
The common mix-up
It is easy to think that placing a sound after a button means the sound will happen once. But the program may reach that sound as soon as it starts, before anyone taps. Put the sound inside the action that belongs to the event, so it waits for the right signal.
Where events appear
Events are behind a camera taking a picture when you tap the shutter, a game jumping when you press a button, and a lamp turning on when a sensor notices darkness. In each case, one thing happens first and starts the response.
Keep exploring
Other languages
Loading MyLeoNes™…