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

#1 2007-06-11 16:28:53

owowo
Scratcher
Registered: 2007-06-11
Posts: 2

Priority of action among sprites

Say two sprites move away if touching each other.  Which sprite takes priority?  I suspect only one of the sprites actually is executed as touching.  Which one?

Offline

 

#2 2007-06-11 19:31:13

kevin_karplus
Scratcher
Registered: 2007-04-27
Posts: 1000+

Re: Priority of action among sprites

It could be either or both---sprites do not appear to have priority.  Whechever script happens to be scheduled first would take priority.  Whether the other script fired or not depends on a race condition (does it manage to detect the touching before the movement brings them apart?)

You should not write code that depens on consistent behavior of race conditions.

Offline

 

#3 2007-06-12 10:16:02

delza
Scratcher
Registered: 2007-03-08
Posts: 26

Re: Priority of action among sprites

owowo wrote:

Say two sprites move away if touching each other.  Which sprite takes priority?  I suspect only one of the sprites actually is executed as touching.  Which one?

I wrote a simple example project to demonstrate how to handle the collision of two sprites: http://scratch.mit.edu/projects/delza/291

Basically, whenever either of them detects a collision, it sends a broadcast message.  Then the sprites turn away when they receive the broadcast, rather than when they collide.  Otherwise, as you've found, one sprite will detect a collision and turn away, then the other will test for collision, but they aren't colliding anymore, so instead of turning it will follow the first sprite (or continue on it's way, depending on which way it was heading).

I hope that helps.

--Dethe

Offline

 

#4 2007-06-12 18:16:41

Mayhem
Scratcher
Registered: 2007-05-26
Posts: 1000+

Re: Priority of action among sprites

Is there not a risk, there, of them both noticing the touch, and the message getting broadcast twice, causign them to turn 180 and turn back again?


Web-spinning Spider:  http://scratch.mit.edu/projects/Mayhem/18456
3D Dungeon Adventure:  http://scratch.mit.edu/projects/Mayhem/23570
Starfighter X: http://scratch.mit.edu/projects/Mayhem/21825
Wandering Knight: http://scratch.mit.edu/projects/Mayhem/28484

Offline

 

#5 2007-06-12 18:44:46

weissjd
Scratcher
Registered: 2007-05-16
Posts: 64

Re: Priority of action among sprites

Instead of having them turn 180 degrees you can have them turn away from each other. I did this by doing a turn towards, then rotate 180 degrees.

Offline

 

Board footer