Hi, I’m making a game and I need help cus it’s not working. The part that isn’t working is a part of the tutorial. The basic concept of the game, for reference, is that each turn you water the tree to make it grow and you try to get points. The tutorial explains more. In the second part of the tutorial I wanted to let the player try cutting a leaf, but it doesn’t work. Here’s the code for that part:
I originally used pause until is B button pressed and cursor hitbox overlaps with leaf, but that didn’t work. It was because the leaf variable was only associated with the latest created sprite using the leaf variable. So I searched online and the code in the screenshot is apparently an alternative. But it doesn’t let me move my cursor (in the game) when it gets up to here.
The problem here is that while loops freeze the game update loop. MakeCode runs game.onUpdate() and controller checks frame by frame, but your while loop hogs the thread. That’s why your cursor won’t move: the game is stuck inside that loop and controller input doesn’t get processed.
Also, pauseUntil() has the same problem:
It pauses everything else, so sprites, animations, and movement stop until the condition is true.
Why your previous “leaf variable only stores the last created sprite” issue happened
Every time you do this, leaf points to the latest leaf only. All the previous leaves still exist, but leaf doesn’t reference them. That’s why looping over sprites.allOfKind(SpriteKind.Leaf) is correct—you need to check all leaves, not just one variable.
How to fix it
You should avoid blocking loops like while or pauseUntil() for player input. Instead, use events or game.onUpdate() checks:
game.onUpdate(function() {
for (let value of sprites.allOfKind(SpriteKind.Leaf)) {
if (controller.B.isPressed() && cursor_hitbox.overlapsWith(value)) {
sprites.destroy(value, effects.ashes, 200)
leaves -= 1
}
}
})
This way:
The game keeps running.
Cursor movement works.
Multiple leaves can be cut at any time.
You can also combine it with a simple debounce if you don’t want B to destroy multiple leaves per frame:
let canCut = true
game.onUpdate(function() {
if (controller.B.isPressed() && canCut) {
for (let value of sprites.allOfKind(SpriteKind.Leaf)) {
if (cursor_hitbox.overlapsWith(value)) {
sprites.destroy(value, effects.ashes, 200)
leaves -= 1
}
}
canCut = false
} else if (!controller.B.isPressed()) {
canCut = true
}
})
That fixes the “game freezes / cursor can’t move” issue and handles all leaves dynamically.