Computer Science
Grade 7
20 min
Multiple Events: Combining Actions
Combine multiple events to create more complex interactions.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Identify situations where multiple events need to be checked simultaneously in a program.
Use logical operators like AND and OR to combine conditions within an event handler.
Design a program that responds to a combination of user inputs, such as pressing two keys at once.
Implement a state-based check in combination with an event (e.g., checking if a character is on the ground before allowing a jump).
Debug programs where the logic for combined events is not working as expected.
Explain the difference between checking for two conditions that must both be true (AND) versus checking if at least one is true (OR).
Have you ever played a video game where you had to press one button to run and another to jump at the same time to clear a huge gap? 🤔 How does t...
2
Key Concepts & Vocabulary
TermDefinitionExample
EventAn action or occurrence detected by the program, such as a mouse click, a key press, or a timer reaching zero.When the user presses the spacebar, that is a 'key press' event.
Event HandlerA block of code that is written to run when a specific event occurs.An 'on spacebar press' block that contains code to make a character jump.
ConditionA statement that the program checks to see if it is true or false. It's often used inside control structures like 'if' statements.The statement 'score > 100' is a condition.
Logical OperatorA symbol or word used to connect two or more conditions to make a more complex decision.The most common logical operators are AND, OR, and NOT.
AND OperatorA logical operator that combines two co...
3
Core Syntax & Patterns
The AND Pattern
if (condition1 AND condition2) {
// Code to run
}
Use this pattern when you need an action to happen only when two or more conditions are met at the exact same time. Both must be true for the code inside the 'if' block to execute.
The OR Pattern
if (condition1 OR condition2) {
// Code to run
}
Use this pattern when you need an action to happen if any one of several conditions is met. Only one of them needs to be true for the code inside the 'if' block to execute.
Event + State Pattern
on event (e.g., key press) {
if (state_variable is true) {
// Code to run
}
}
This is a powerful way to combine an immediate user action (an event) with a background condition (a state). It prevents actions from happening at the wr...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
To create a 'sprint' mechanic, a player holds 'Shift'. Sprinting drains a `stamina` bar. The player should stop sprinting if they release 'Shift' OR if `stamina` reaches 0. Which set of logic correctly implements this?
A.`on 'Shift' press { isSprinting = true }`. The program will never stop sprinting.
B.`if (key 'Shift' is pressed AND stamina > 0) { sprint() }`. This works perfectly on its own.
C.`if (key 'Shift' is pressed AND stamina > 0) { sprint() } else { stopSprinting() }`. This correctly checks the conditions and provides a way to stop.
D.`on 'Shift' press { sprint() }; on 'Shift' release { stopSprinting() }`. This doesn't account for the stamina running out.
Challenging
A puzzle requires a player to stand on a blue tile (`onBlueTile` is true) and then press a red button (`onRedButton` is true). The code is `if (onBlueTile is true OR onRedButton is true) { solve puzzle }`. Players are solving the puzzle just by touching the red button. How should the code be fixed?
A.`if (onBlueTile is true AND onRedButton is true)`
B.`if (onBlueTile is true) { if (onRedButton is true) { solve puzzle } }`
C.`on blue tile touch { on red button press { solve puzzle } }`
D.The code is correct; the bug must be somewhere else.
Challenging
A wizard can cast a powerful spell. The conditions are: the 'C' key is pressed, the `mana` variable is over 90, and the `isSilenced` variable is false. Which `if` statement correctly combines all three requirements?
A.`if (key 'C' is pressed AND (mana > 90 OR isSilenced is false))`
B.`if ((key 'C' is pressed OR mana > 90) AND isSilenced is false)`
C.`if (key 'C' is pressed AND mana > 90 AND isSilenced is false)`
D.`if (key 'C' is pressed OR mana > 90 OR isSilenced is false)`
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free