This is a read-only archive of the old Scratch 1.x Forums.
Try searching the current Scratch discussion forums.

#1 2011-05-18 17:49:45

estorken
Scratcher
Registered: 2010-04-01
Posts: 41

movement, forever vs when clicked

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?


http://i56.tinypic.com/esqpma.gif

Offline

 

#2 2011-05-18 19:03:16

Pecola1
Scratcher
Registered: 2010-09-06
Posts: 1000+

Re: movement, forever vs when clicked

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.  smile


If you are reading this, please read to the end, because if you don't you won't know what's at the end. Don't just skip to the end though otherwise you won't be able to read the middle, which is most important. Now you must be wondering why you just read all that, the reason is you may have not noticed something, read it again and see if you notice it this time  smile

Offline

 

#3 2011-05-18 21:11:41

estorken
Scratcher
Registered: 2010-04-01
Posts: 41

Re: movement, forever vs when clicked

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


http://i56.tinypic.com/esqpma.gif

Offline

 

#4 2011-05-19 07:35:15

Pecola1
Scratcher
Registered: 2010-09-06
Posts: 1000+

Re: movement, forever vs when clicked

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.  smile  I hope you understand me better.


If you are reading this, please read to the end, because if you don't you won't know what's at the end. Don't just skip to the end though otherwise you won't be able to read the middle, which is most important. Now you must be wondering why you just read all that, the reason is you may have not noticed something, read it again and see if you notice it this time  smile

Offline

 

#5 2011-05-19 12:35:01

LS97
Scratcher
Registered: 2009-06-14
Posts: 1000+

Re: movement, forever vs when clicked

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

 

#6 2011-05-19 19:56:13

Harakou
Community Moderator
Registered: 2009-10-11
Posts: 1000+

Re: movement, forever vs when clicked

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.


http://www.blocks.scratchr.org/API.php?action=random&return=image&link1=http://i.imgur.com/OZn2RD3.png&link2=http://i.imgur.com/duzaGTB.png&link3=http://i.imgur.com/CrDGvvZ.png&link4=http://i.imgur.com/POEpQyZ.png&link5=http://i.imgur.com/ZKJF8ac.png

Offline

 

#7 2011-05-20 17:06:28

WololoW
Scratcher
Registered: 2011-05-19
Posts: 8

Re: movement, forever vs when clicked

<when green flag clicked>
<forever>
<key[WHATEVER]pressed?><if>
<change{X}by(1
<end>

Offline

 

#8 2011-05-20 18:58:25

estorken
Scratcher
Registered: 2010-04-01
Posts: 41

Re: movement, forever vs when clicked

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)


http://i56.tinypic.com/esqpma.gif

Offline

 

Board footer