Discuss Scratch
- Discussion Forums
- » Help with Scripts
- » what's the difference between 'key pressed' and 'when key pressed'?
- hellotommy
-
New Scratcher
2 posts
what's the difference between 'key pressed' and 'when key pressed'?
I am practicing making a street fighter role's movement, the practice is ,when there is no any control on the role, the role's on a stand state, and need to play stand animation, and when left or right arrow button is pressed, the role need to make corresponding moves and play walking animation.
When I used the method below , the movement became lag, not smooth, would like to ask experts here, how is this going on?
The method is ,
1.put an ‘next costume’ on a forever loop to play the stand animation, then attached to ‘when flag clicked’ ,
2.put an ‘next costume’ on a loop to play the walking animation ,then attached to corresponding ‘when key pressed’,
eg. After clicked the flag button, the role is on a stand state animation loop, when left arrow button pressed and not release, break the loop and play walking animation , however, the walking animation played lag,not smooth.
I saw many people used ‘key pressed’ in a forever loop to control sprite's movement, and the effect is good.
what's the differences between those two kind of method, is ‘when key pressed’ have weak point on breaking other loop ?
Thanks.
When I used the method below , the movement became lag, not smooth, would like to ask experts here, how is this going on?
The method is ,
1.put an ‘next costume’ on a forever loop to play the stand animation, then attached to ‘when flag clicked’ ,
2.put an ‘next costume’ on a loop to play the walking animation ,then attached to corresponding ‘when key pressed’,
eg. After clicked the flag button, the role is on a stand state animation loop, when left arrow button pressed and not release, break the loop and play walking animation , however, the walking animation played lag,not smooth.
I saw many people used ‘key pressed’ in a forever loop to control sprite's movement, and the effect is good.
what's the differences between those two kind of method, is ‘when key pressed’ have weak point on breaking other loop ?
Thanks.
- footsocktoe
-
Scratcher
1000+ posts
what's the difference between 'key pressed' and 'when key pressed'?
I am practicing making a street fighter role's movement, the practice is ,when there is no any control on the role, the role's on a stand state, and need to play stand animation, and when left or right arrow button is pressed, the role need to make corresponding moves and play walking animation.
When I used the method below , the movement became lag, not smooth, would like to ask experts here, how is this going on?
The method is ,
1.put an ‘next costume’ on a forever loop to play the stand animation, then attached to ‘when flag clicked’ ,
2.put an ‘next costume’ on a loop to play the walking animation ,then attached to corresponding ‘when key pressed’,
eg. After clicked the flag button, the role is on a stand state animation loop, when left arrow button pressed and not release, break the loop and play walking animation , however, the walking animation played lag,not smooth.
I saw many people used ‘key pressed’ in a forever loop to control sprite's movement, and the effect is good.
what's the differences between those two kind of method, is ‘when key pressed’ have weak point on breaking other loop ?
Thanks.
“When key pressed” is designed to trigger something once, so it has a slight delay built into it to prevent double triggering.
“If key pressed” only detects if the key is currently pressed or not.
You would use an “if key pressed” loop for walking, but for a jump you might use a “when key pressed”.
“When key pressed” for shot gun, but “if key pressed” loop for machine gun.
- DadOfMrLog
-
Scratcher
1000+ posts
what's the difference between 'key pressed' and 'when key pressed'?
“When key pressed” is designed to trigger something once, so it has a slight delay built into it to prevent double triggering.There's no delay in terms of pressing a key – when you press the key, the “when key pressed” script triggers as soon as Scratch gets around to scheduling the script after it notices there was a key-down event. That'll be during the same refresh as the “key pressed?” sensor block gets ‘switched on’ (i.e. becomes true).
(I'll also note that a “when key pressed” script will not trigger again if there is another key-down or key-repeat event while its script is still running. There can't be any ‘double-triggering’ simply for that reason – which is the opposite of the “when this sprite clicked” block, as well as “when I receive” and “when backdrop switches”…)
However, one important difference with “when key pressed” is that the block is responding to events from your Operating System – both ‘key down’ and ‘key repeat’. Key-down is the event that happens when you first press a key. Key-repeat is an event that happens, regularly spaced in time, if you hold down a key.
That means the behaviour of “when key pressed” is actually dependent on your settings on your computer! (For example, if you switch off key-repeat in your OS prefs/settings then the “when key pressed” script will only trigger *once* when you press and hold down the key. And if, instead, you have your key-repeat speed set to, say, once per second, then the “when key pressed” script will trigger once per second after the initial first press of the key.)
This is totally different to the “key pressed?” sensor block, which is true at any point in time that you are holding down the key. (Except for issues of keyboard ghosting, of course.)
Think of the “key pressed?” block as being like an immediate question that gets an answer right now about whether or not the key is down. While the “when key pressed” block means “when the Operating System sends me a key-down or key-repeat event” (which has the behaviour that you would see when holding down a key while typing in a word-processing window, for example).
You can see a clear difference in the behaviour of these two in this demo project:

https://scratch.mit.edu/projects/18453174/
Does that help?
Last edited by DadOfMrLog (Feb. 21, 2017 21:15:07)
- hellotommy
-
New Scratcher
2 posts
what's the difference between 'key pressed' and 'when key pressed'?
it really helps ,thanks so much @DadOfMrLog
- Discussion Forums
- » Help with Scripts
-
» what's the difference between 'key pressed' and 'when key pressed'?



