Discuss Scratch

It5MeCohen
Scratcher
2 posts

toggle menu

I'm tryna make a toggle menu but when I press G it doesn't open the menu

when green flag clicked
hide
set (shop) to [0]
forever
if <key [g v] pressed?> then
wait until <not <key [g v] pressed?>>
if <not <(shop) = [1]>> then
set [shop v] to [1]
show
end
if <(shop) = [1]> then
set [shop v] to [0]
hide
end
end
end

whats the issue
Scratch-Minion
Scratcher
1000+ posts

toggle menu

The issue occurs when shop = 0.
Your first “If” block will set shop to 1, then the second “If” block will immediately set shop back to 0.

You should use an “if … then … else …” block so that only one of the if blocks runs.


when green flag clicked
hide
set (shop) to [0]
forever
if <key [g v] pressed?> then
wait until <not <key [g v] pressed?>>
if <not <(shop) = [1]>> then
set [shop v] to [1]
show
else
set [shop v] to [0]
hide
end
end
end

or, more simply:

when green flag clicked
hide
set (shop) to [0]
forever
if <key [g v] pressed?> then
wait until <not <key [g v] pressed?>>
if <(shop) = [0]> then
set [shop v] to [1]
show
else
set [shop v] to [0]
hide
end
end
end
ramenecho
Scratcher
100+ posts

toggle menu

It5MeCohen wrote:

I'm tryna make a toggle menu but when I press G it doesn't open the menu

when green flag clicked
hide
set (shop) to [0]
forever
if <key [g v] pressed?> then
wait until <not <key [g v] pressed?>>
if <not <(shop) = [1]>> then
set [shop v] to [1]
show
end
if <(shop) = [1]> then
set [shop v] to [0]
hide
end
end
end

whats the issue
While yes, Scratch-Minion’s solution works perfectly, another thing that might help is, if possible, reducing it to a when a key is pressed, having a wait until block in a forever loop could interfere with a lot of things, so this would be a little more efficient
when green flag clicked
hide
set (shop) to [0]

when [G v] key pressed
if <(shop) = [0]> then
set [shop v] to [1]
show
else
set [shop v] to [0]
hide
end

Not only does this remove a wait until block, it will also reduce the amount of if blocks being executed per loop
Little bit of nerdy info here
(If else) and (if) blocks are the largest cause of lag, having them in a forever loop will lead to it activating an (if) block every frame, even if the condition is false, the original script also had 2 (if) blocks, so even more. When it is put in the (when key pressed) block, the script will only check the if block when that key gets pressed, therefore reducing the total lag. While it may not seem like much, stuff like that can build up. It’s also better to have that knowledge for any future large projects you might make

Hello there, this is a cool signature! You can even make your own wow! I really need somebody to check my game for bugs, MineQuest Idle its not the highest quality of games, but its an improvement from my previous ones

Powered by DjangoBB