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

#1 2012-07-02 14:15:29

LillaMy
New Scratcher
Registered: 2012-06-29
Posts: 2

What is logically wrong with this script?

Hi everyone,

this may be a total noob question. I've got a chunk of pretty basic script that doesn't work the way I expected it to. I've found a way to make it work, so I don't need suggestions for that, but I'd like to understand why the first thing I tried didn't work. Just for curiosity's sake, really.

Basically what I have is two sprites, one of which moves around randomly, the other one I control. The controlled one chases the other one, when it "catches" (touches) it, the random one disappears.

So the code that didn't work for the randomly moving sprite was:

when gf clicked
show
repeat until <touching [other sprite]>
glide <pick random [3] to [8]> secs to x: <pick random [-240] to [240]> y: <pick random [-180] to [180]>
wait [0.1] secs
end
hide
With this, when the sprites touch, sometimes the random one does hide. Sometimes it hides after a few seconds of "being touched", and sometimes it doesn't hide at all.

In my logic, the code  basically says: Do the moving around stuff UNTIL you touch the other sprite. WHEN you touch the other sprite, hide.

So, can somebody explain to me why that isn't the case? I.e. translate computer-logic to me-logic?  big_smile

Thank you!

Offline

 

#2 2012-07-02 14:22:58

SciTecCf
Scratcher
Registered: 2011-11-23
Posts: 1000+

Re: What is logically wrong with this script?

How the repeat until block works is, at the end of the loop, it does this check: if the argument is false, it repeats the loop. If it is true, it breaks out of the loop. So, is checks at the END of the loop code whether it is touching or not.

Last edited by SciTecCf (2012-07-02 14:24:09)


http://bit.ly/LCZEJRhttp://bit.ly/LSONcOhttp://bit.ly/LF3vIc
http://trinary.site40.net/images/scratchrank.php?username=SciTecCf&amp;display=small

Offline

 

#3 2012-07-02 14:23:09

Borrego6165
Scratcher
Registered: 2011-03-10
Posts: 1000+

Re: What is logically wrong with this script?

problem is it only checks every 0.1 seconds, and during gliding it doesn't check either. how about instead "point towards sprite" (set rotation off firstly so it appears straight) then "move X steps" and it should follow AND check (get rid of wait 0.1 seconds)


Generation:4001 Build a beautiful city, with over 50 objects and over 10000 tiles per city! This simulates traffic, pollution, tourism, crime and more!

Offline

 

#4 2012-07-02 14:23:17

Firedrake969
Scratcher
Registered: 2011-11-24
Posts: 1000+

Re: What is logically wrong with this script?

Can you upload the project for me to see?


Click the sign.
https://s3.amazonaws.com/eterna/eterna2/logo2.png

Offline

 

#5 2012-07-02 14:24:13

Borrego6165
Scratcher
Registered: 2011-03-10
Posts: 1000+

Re: What is logically wrong with this script?

SciTecCf wrote:

It's the "wait 0.1 secs" block. How the repeat until block works is, at the end of the loop, it does this check: if the argument is false, it repeats the loop. If it is true, it breaks out of the loop. So, is checks at the END of the loop code whether it is touching or not.

i thought it checks at the start, which is why on my scripts it doesn;t repeat if it is not necessary to. btw, don;t forget to read my explanation above!


Generation:4001 Build a beautiful city, with over 50 objects and over 10000 tiles per city! This simulates traffic, pollution, tourism, crime and more!

Offline

 

#6 2012-07-02 14:24:37

SciTecCf
Scratcher
Registered: 2011-11-23
Posts: 1000+

Re: What is logically wrong with this script?

Borrego6165 wrote:

problem is it only checks every 0.1 seconds, and during gliding it doesn't check either. how about instead "point towards sprite" (set rotation off firstly so it appears straight) then "move X steps" and it should follow AND check (get rid of wait 0.1 seconds)

Actually, it checks every 1.1 secs.  wink


http://bit.ly/LCZEJRhttp://bit.ly/LSONcOhttp://bit.ly/LF3vIc
http://trinary.site40.net/images/scratchrank.php?username=SciTecCf&amp;display=small

Offline

 

#7 2012-07-02 14:26:55

Borrego6165
Scratcher
Registered: 2011-03-10
Posts: 1000+

Re: What is logically wrong with this script?

SciTecCf wrote:

Borrego6165 wrote:

problem is it only checks every 0.1 seconds, and during gliding it doesn't check either. how about instead "point towards sprite" (set rotation off firstly so it appears straight) then "move X steps" and it should follow AND check (get rid of wait 0.1 seconds)

Actually, it checks every 1.1 secs.  wink

well that makes it worse!  big_smile  which is why you need to get rid of "glide to" and replace with the other scripts.

Last edited by Borrego6165 (2012-07-02 14:27:37)


Generation:4001 Build a beautiful city, with over 50 objects and over 10000 tiles per city! This simulates traffic, pollution, tourism, crime and more!

Offline

 

#8 2012-07-02 15:11:35

LillaMy
New Scratcher
Registered: 2012-06-29
Posts: 2

Re: What is logically wrong with this script?

Oooooohhhhh.....!

Yes, that makes perfect sense - it does stuff inside the loop for anything from roughly 3 - 9 seconds before it ever even loops back around to check if the touching statement is true, that's why it would sometimes hide quite soon, and sometimes take a while.

Gee, that was really quite obvious actually  big_smile

Thanks so much, guys!

Offline

 

#9 2012-07-04 14:34:55

skippito
Scratcher
Registered: 2012-03-17
Posts: 100+

Re: What is logically wrong with this script?

The problem with your script is that the sprite glides. It could touch the other one halfway through without hiding because it is still moving. Basically, repeat-until scripts cannot stop while running the thing inside of them. What they do is check if something is true. If it is they do nothing and stop looping. Otherwise, they run what is inside of them, loop, and check again.

Offline

 

#10 2012-07-05 01:29:53

JH1010
Scratcher
Registered: 2012-05-31
Posts: 1000+

Re: What is logically wrong with this script?

Put the hide in another script:

when gf clicked
forever if <touching sprite 2>
hide
end
Change the other script to this:

When gf clicked
glide <pick random (3) to (8)> secs to x: <pick random (-240) to (240)> y: <pick random (-180) to (180)>
Hope this helps.

EDIT: I became a scratcher whilst posting this!

Last edited by JH1010 (2012-07-05 01:33:15)

Offline

 

Board footer