Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » When space key pressed not working
- thesenatebruh
-
Scratcher
9 posts
When space key pressed not working
So, I'm making a project. I wanted the game to start when you pressed space. I do not want you to bump space and have to start over. So, I came up with this:
when green flag clickedAnd before you ask, yes, I have another block that will change beginning to 1 when flag clicked. Code does not work. I dunno why. Any thoughts?
go to x: (43) y: (13)
if <(beginning) = [1]> then
if <key [space] pressed?> then
set (beginning) to [0]
stop all sounds
broadcast [start game]
end
end
- Coder046
-
Scratcher
100+ posts
When space key pressed not working
You are only checking for the key being pressed once, at the beginning of the project, in one single frame. It will work if you are holding down space when you press the green flag, but after that, it doesn't check. I would recommend something more along these lines:
when green flag clickedThis waits over multiple frames until a key is pressed, and then checks if the space key is pressed. However, if that key is not pressed, the script keeps running until the space key is pressed. Here's another version:
go to x: (43) y: (13)
forever
wait until <key (any v) pressed?>
if <key (space v) pressed?> then
set [beginning v] to [0]
stop all sounds
broadcast (start game v)
stop [this script v]
end
end
when green flag clicked
go to x: (43) y: (13)
wait until <key (space v) pressed?>
set [beginning v] to [0]
stop all sounds
broadcast (start game v)
- thesenatebruh
-
Scratcher
9 posts
When space key pressed not working
You are only checking for the key being pressed once, at the beginning of the project, in one single frame. It will work if you are holding down space when you press the green flag, but after that, it doesn't check. I would recommend something more along these lines:Thanks!when green flag clickedThis waits over multiple frames until a key is pressed, and then checks if the space key is pressed. However, if that key is not pressed, the script keeps running until the space key is pressed. Here's another version:
go to x: (43) y: (13)
forever
wait until <key (any v) pressed?>
if <key (space v) pressed?> then
set [beginning v] to [0]
stop all sounds
broadcast (start game v)
stop [this script v]
end
endwhen green flag clicked
go to x: (43) y: (13)
wait until <key (space v) pressed?>
set [beginning v] to [0]
stop all sounds
broadcast (start game v)
- Discussion Forums
- » Help with Scripts
-
» When space key pressed not working