is it better to move with when key clicked repeat until or with a forever if.
you can use
when green flag clicked
forever
{
if (right arrow pressed)
{
change x by 10
}
if (left arrow key pressed)
{
change x by -10
}
}
or
when (right arrow key pressed)
repeat until (not right arrow key pressed0
{
change x by 10
}
when (left arrow key pressed)
repeat until (not left arrow key pressed0
{
change x by -10
}
i think that the first one responds faster but causes constant lag. and the second one is pretty much the opposite.
i normally use the second unless it's a small game or i need a forever anyway. is this correct?
Offline
HAT BLOCK - The when key pressed hat block if up or down arrows are chosen will sense if the mouses middle button is scrolled up or down. However hat blocks stop their script every-time before completing the script, so if you had:
when key pressed
move 1 step
wait one second
move 2 steps
If you held down the key, it would skip all after the first block until the button is released. So the script you put would not work too well.
FOREVER - The forever and if key pressed repeats if the key is pressed, otherwise it will skip the if, and then do it if it becomes true again. The good news: this will make it so it will finish the script then if the condition is true it will do it again, the bad news, if there is another if, the one if will not work until all the other ifs are done. So if you use this, I would make more than one hat block/forever/if key pressed/move scripts so you can press the left arrow while pressing the up key. (though your script looks good).
On your script, go with the forever if, it will work better and look more professional.
Offline
estorken wrote:
thank you. i don't really understand your comment on the hat blocks, i thought a repeat until button released would fix that, but OK. i'll take your advice
You see, een with a repeat until, the whole script will only play the first part, if you pressed and held the button, Scratch would read it like so:
when key pressed
repeat until
when key pressed
repeat until
when key pressed
repeat until...
Because each time you press the key, it has to restart the whole script. I hope you understand me better.
Offline
About you hat block question:
Scratch developers explicitly said coded certain hat blocks (the green flag, keys and receive) to stop what they're doing and restart if they get the event again.
Offline
LS97 wrote:
About you hat block question:
Scratch developers explicitly said coded certain hat blocks (the green flag, keys and receive) to stop what they're doing and restart if they get the event again.
Exactly. It creates a noticeable delay before it starts repeating, and makes everything pretty much the opposite of smooth. Definitely go with the forever if.
Offline
but... i thought it only did that with when i receive and green flag clicked scripts. i just tested it and it doesn't happen with the key pressed hat block, for me at least. idk why, but i can press it however often and however many times, and it just keeps running. i do however agree with the pause before repeating one though. but i'm still not sure whether it is better to use them though, because it causes lag throughout the project.
additional question though, if i only want you to be able to move during a level, but not during the menu, would it be better to say
forever
{
if(right arrow pressed and level = 1)
{
change x by 5
}
}
or to say
when i receive level
repeat until (level = 0)
{
if(right arrow pressed and level = 1)
{
change x by 5
}
}
or would it, in this case. be better to use a when key pressed hat block and say
when right arrow pressed
if (level = 1)
{
repeat until (not right arrow pressed)
{
change x by 5
}
}
in this case they will all have to check something anyway.
i appreciate your time and energy
Last edited by estorken (2011-05-20 19:01:01)
Offline