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

#1 2012-09-20 12:05:53

JulietBrown
New Scratcher
Registered: 2012-09-19
Posts: 7

Peculiar behaviour of 'repeat until'

I have an apple falling out of a tree. Player needs to 'mouse down' and drag it into a basket.  My code has a repeat until (mouse-down and distance to mouse-pointer is 0) loop, inside which the apple falls 10 units each 0.1 second. When I put the mouse down on the apple it stops moving ... so far so good ... but the code DOESN'T exit the loop and continue to the code following.  It just sits there waiting, and when I release the mouse (mouse-up) it goes back to the top of the loop and carries on as before.  This seems pretty odd to me!  All repeat loops in my past exit when the 'until' condition becomes true ... which seems to be the point of them?

Offline

 

#2 2012-09-20 15:33:09

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Peculiar behaviour of 'repeat until'

JulietBrown wrote:

I have an apple falling out of a tree. Player needs to 'mouse down' and drag it into a basket.  My code has a repeat until (mouse-down and distance to mouse-pointer is 0) loop, inside which the apple falls 10 units each 0.1 second. When I put the mouse down on the apple it stops moving ... so far so good ... but the code DOESN'T exit the loop and continue to the code following.  It just sits there waiting, and when I release the mouse (mouse-up) it goes back to the top of the loop and carries on as before.  This seems pretty odd to me!  All repeat loops in my past exit when the 'until' condition becomes true ... which seems to be the point of them?

The reapeat until loops do repeat until a condition becomes true, so I think that this is a problem with your script. Could you post your script so that [other scratchers] and I could look at it and see what 's going wrong?


http://i46.tinypic.com/35ismmc.png

Offline

 

#3 2012-09-21 03:55:46

JulietBrown
New Scratcher
Registered: 2012-09-19
Posts: 7

Re: Peculiar behaviour of 'repeat until'

Well, I must be being a prize prat.  You are of course right, and there's something wrong with the loop condition.  I'm now trying to simplify the code to just demonstrate the loop itself rather than dumping miles of stuff on you.  If I don't see the light during doing that, I will post the code.  Thanks

Offline

 

#4 2012-09-21 04:43:36

JulietBrown
New Scratcher
Registered: 2012-09-19
Posts: 7

Re: Peculiar behaviour of 'repeat until'

After more investigation I find that the following test code for the falling apple
does work as expected.  The looptermination condition is just 'until mouse down'.

when I receive [setup]
repeat until <mouse down?>
change x by (-10)
wait (0.1) secs
end
say [Hello] for (2) secs
But the problem is that I don't want the loop to exit on mouse-down unless the mouse is down on the actual apple sprite: no good if it's just anywhere on the stage.  So, I optimistically added to the condition, like this: (the condition is now that the mouse is down and the distance from the mouse pointer to the sprite is zero)

when I receive [setup]
repeat until <<mouse down?> and <distance to [mouse-pointer] = (0)>>
change x by (-10)
wait (0.1) secs
end
say [Hello] for (2) secs
Now it doesn't work ... loop starts up again as soon as the mouse is released.  So the problem changes from 'behaviour of repeat loop' to 'meaning of distance to mouse pointer'.  I'm obviously using this construct incorrectly.  Can you advise?

Offline

 

#5 2012-09-21 06:17:22

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Peculiar behaviour of 'repeat until'

JulietBrown wrote:

After more investigation I find that the following test code for the falling apple
does work as expected.  The looptermination condition is just 'until mouse down'.

when I receive [setup]
repeat until <mouse down?>
change x by (-10)
wait (0.1) secs
end
say [Hello] for (2) secs
But the problem is that I don't want the loop to exit on mouse-down unless the mouse is down on the actual apple sprite: no good if it's just anywhere on the stage.  So, I optimistically added to the condition, like this: (the condition is now that the mouse is down and the distance from the mouse pointer to the sprite is zero)

when I receive [setup]
repeat until <<mouse down?> and <(distance to [mouse-pointer v]?) = (0)>>
change x by (-10)
wait (0.1) secs
end
say [Hello] for (2) secs
Now it doesn't work ... loop starts up again as soon as the mouse is released.  So the problem changes from 'behaviour of repeat loop' to 'meaning of distance to mouse pointer'.  I'm obviously using this construct incorrectly.  Can you advise?

The distance conditional you were using meant that the mouse had to be directly over the apple to sense a click. What I would use is this:

when I receive [setup v]
repeat until <<mouse down?> and <touching [mouse-pointer v]?>>
 change x by (-10)
 wait (0.1) secs
end
say [Hello] for (2) secs


http://i46.tinypic.com/35ismmc.png

Offline

 

#6 2012-09-21 06:39:37

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Peculiar behaviour of 'repeat until'

JulietBrown wrote:

After more investigation I find that the following test code for the falling apple
does work as expected.  The looptermination condition is just 'until mouse down'.

when I receive [setup]
repeat until <mouse down?>
change x by (-10)
wait (0.1) secs
end
say [Hello] for (2) secs
But the problem is that I don't want the loop to exit on mouse-down unless the mouse is down on the actual apple sprite: no good if it's just anywhere on the stage.  So, I optimistically added to the condition, like this: (the condition is now that the mouse is down and the distance from the mouse pointer to the sprite is zero)

when I receive [setup]
repeat until <<mouse down?> and <(distance to [mouse-pointer v]) = (0)>>
change x by (-10)
wait (0.1) secs
end
say [Hello] for (2) secs
Now it doesn't work ... loop starts up again as soon as the mouse is released.  So the problem changes from 'behaviour of repeat loop' to 'meaning of distance to mouse pointer'.  I'm obviously using this construct incorrectly.  Can you advise?

The flaw I see is that the distance condition you were using meant that the mouse had to be directly over the apple to sense a click. What I would use is this:

when I receive [setup v]
repeat until <<mouse down?> and <touching [mouse-pointer v]?>>
 change x by (-10)
 wait (0.1) secs
end
say [Hello] for (2) secs

Last edited by ErnieParke (2012-09-21 06:46:00)


http://i46.tinypic.com/35ismmc.png

Offline

 

#7 2012-09-21 10:20:30

JulietBrown
New Scratcher
Registered: 2012-09-19
Posts: 7

Re: Peculiar behaviour of 'repeat until'

Yes, thanks, that's obviously a better way.  Thank you v much for your help.  Scratch is fun!

Offline

 

#8 2012-09-21 14:53:38

ErnieParke
Scratcher
Registered: 2010-12-03
Posts: 1000+

Re: Peculiar behaviour of 'repeat until'

JulietBrown wrote:

Yes, thanks, that's obviously a better way.  Thank you v much for your help.  Scratch is fun!

Scratch is always fun! Anyway, your welcome!


http://i46.tinypic.com/35ismmc.png

Offline

 

Board footer