| Level Navigation: 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18⚡ | 19 |
User Story: As a player, I want to lose a life when I miss the apple so that the game has challenge and consequences.
Add a lives state variable and create a miss handler for clicking the background.
lives with initial value of 3missTarget function that decrements lives by 1onClick handler to the orchard-background divNeed help with multiple state variables? Check out these snippets:
function App() {
const [score, setScore] = useState(0)
const [lives, setLives] = useState(3) // NEW
// ... rest of component
}
function missTarget() {
setLives(lives - 1)
}
return (
<>
<div className="stats">
Score: {score} Lives: {lives}
</div>
<div className='orchard-background' onClick={missTarget}>
<div className="apple-target" onClick={clickTarget}></div>
</div>
</>
)
event.stopPropagation() is in clickTargetUnderstanding Event Propagation:
Try removing event.stopPropagation() temporarily to see what happens. You’ll notice clicking the apple triggers BOTH handlers because clicks “bubble up” from child to parent. That’s why we need stopPropagation()!