Discuss Scratch

RT_Borg
Scratcher
1000+ posts

Button cooldowns

Hi plasmaticatt, Welcome to Scratch!

There's an important lesson that crops up over and over again in Scratch. If you have two things you'd like to do, and one needs to be paused, they belong in separate scripts. This often comes up when you have motion and costume animation, but here you're seeing it with motion and keyboard handling.

What you'd like to do is pause input from the space key while it's held down, but that pause is undesirable for the motion (change y by) which should not be paused.

In code, you have:

when I start as a clone
forever
change y by (-10)
if <touching [edge v]?> then
change [score v] by (-1)
delete this clone
end
if <key [space v] pressed?> then // putting a wait until not touching space halts the whole loop
if <not <touching [Sprite2 v]?>> then
change [score v] by (-1)
end
if <touching [Sprite2 v]?> then
if <key [space v] pressed?> then
change [score v] by (1)
delete this clone
end
end
end
end

What you want is this

when I start as a clone
forever
change y by (-10)
if <touching [edge v]?> then
change [score v] by (-1)
delete this clone
end
end

when I start as a clone
forever
wait until <not <key [space v] pressed?>> // go from unpressed
wait until <key [space v] pressed?> // to pressed
if <touching [Sprite2 v]?> then
change [score v] by (1)
delete this clone
else
change [score v] by (-1)
end
end

Make sure that second script is “when I start as a clone” since it's the falling clone you want reacting to the space key.

I hope this helps,

– RT_Borg
RT_Borg
Scratcher
1000+ posts

Button cooldowns

I see while I was typing my message, you were making changes and tried this:

when [space v] key pressed
if <not <touching [Sprite2 v]?>> then
change [score v] by (-1)
end
if <touching [Sprite2 v]?> then
change [score v] by (1)
delete this clone

I imagine you're confused by the result that you never seem to score points, and when you miss it scores -2. (in addition to it not waiting)

This is happening because both the original (non-clone) and the clone are both responding to “when space key pressed”. The non-clone is up at the top of the screen, never touching the target, so when the non-clone reacts to “when space key pressed” it changes the score by -1. When the clone reacts to “when space key pressed” it sometimes misses and contributes another -1 (total -2) and sometimes is touching the target, contributing +1 (for a total of 0).

Here's a post from Help with Scripts about the same situation, but when clones and the non-clone respond to “when I receive”, which works the same as “when key pressed”:

https://scratch.mit.edu/discuss/post/6465989/

Happy to answer any questions,

– RT_Borg
DeveloperTools
Scratcher
100+ posts

Button cooldowns

plasmaticatt wrote:

it didnt work
You must be doing it wrong, because it works for me

Powered by DjangoBB