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

#1 2008-06-06 14:31:58

whatisnttaken
Scratcher
Registered: 2008-06-06
Posts: 5

How do I make two sprites avoid each ohter?

Basically the title. In my particular case I am trying to make one sprite "sense" the other. As they go across the screen if one sprite touches the other I want to make that sprite (the one in motion) move up or down (depending on which motion would cause the one to avoid the other) a set value, and continue on its course across the screen.

Also is there any way for one sprite to "sense" the distance from it to the other?

Offline

 

#2 2008-06-06 14:39:19

dbal
Scratcher
Registered: 2007-07-19
Posts: 100+

Re: How do I make two sprites avoid each ohter?

whatisnttaken wrote:

Basically the title. In my particular case I am trying to make one sprite "sense" the other. As they go across the screen if one sprite touches the other I want to make that sprite (the one in motion) move up or down (depending on which motion would cause the one to avoid the other) a set value, and continue on its course across the screen.

Also is there any way for one sprite to "sense" the distance from it to the other?

[blocks]
<touching[ other sprite
<distance to[ other sprite
[/blocks]

Note that in both cases above, you need to select the other sprite from the pulldown list that is built into the block.  Also note that the other sprite must have been previously added to the project to be included in the pulldown list.

Last edited by dbal (2008-06-06 14:40:35)


Dick Baldwin - Don't get stuck scratching. When you master Scratch, move on up to more serious programming languages. Free online programming tutorials:
Scratch - Alice - Java - C# - C++ - JavaScript - XML - Python - DSP

Offline

 

#3 2008-06-06 16:31:46

whatisnttaken
Scratcher
Registered: 2008-06-06
Posts: 5

Re: How do I make two sprites avoid each ohter?

Say I wanted to have one sprite perform a certain function at a set distance from another sprite? Is that possible?

Offline

 

#4 2008-06-06 16:36:25

coolstuff
Community Moderator
Registered: 2008-03-06
Posts: 1000+

Re: How do I make two sprites avoid each ohter?

if distance to (sprite) < 20
   broadcast (myfunction)
=====================

Offline

 

#5 2008-06-06 16:38:40

dbal
Scratcher
Registered: 2007-07-19
Posts: 100+

Re: How do I make two sprites avoid each ohter?

whatisnttaken wrote:

Say I wanted to have one sprite perform a certain function at a set distance from another sprite? Is that possible?

Yes, but at this point, I am going to refer you to a set of online tutorials and let you start studying about how to program in Scratch instead of just giving you the solution.  Click on the link to Scratch in my signature below.


Dick Baldwin - Don't get stuck scratching. When you master Scratch, move on up to more serious programming languages. Free online programming tutorials:
Scratch - Alice - Java - C# - C++ - JavaScript - XML - Python - DSP

Offline

 

#6 2008-06-09 12:28:19

whatisnttaken
Scratcher
Registered: 2008-06-06
Posts: 5

Re: How do I make two sprites avoid each ohter?

This is the script I have. When I move sprite 3 near sprite 2 nothing happens. What mistake have I made? I tried to follow the script as outlined by coolstuff.
<when green flag clicked><forever if><(  <>> 50 )><distance to[ sprite2]<forever><broadcast[ move]

<when I receive[move]<repeat(10)<wait(.1<change y by(10  )secsc><stop script>

Offline

 

#7 2008-06-09 17:20:04

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

Re: How do I make two sprites avoid each ohter?

Upload your project as it stands and we can have a look at the problem more easily.


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

 

#8 2008-06-11 17:24:32

Cyfarwyddyd
Scratcher
Registered: 2008-06-04
Posts: 17

Re: How do I make two sprites avoid each ohter?

it's actually a complicated problem. here's a simple way to do it, where one sprite rotates when it gets too close to the other one (the direction it rotates depends upon which side is closer to the bad guy).

http://scratch.mit.edu/projects/Cyfarwyddyd/186136

as mayhem says, there's nothing better than posting it, though.

Offline

 

#9 2008-06-11 18:07:35

Cyfarwyddyd
Scratcher
Registered: 2008-06-04
Posts: 17

Re: How do I make two sprites avoid each ohter?

actually, i see from the first post that you just want to make the sprite go back and forth. that makes it easier:
http://scratch.mit.edu/projects/Cyfarwyddyd/186184

Offline

 

#10 2008-06-13 16:12:04

75zx
Scratcher
Registered: 2007-07-09
Posts: 6

Re: How do I make two sprites avoid each ohter?

<when green flag clicked><forever><if><touching[ <change x by( -0.1 OR 0.1

Offline

 

#11 2008-06-17 12:41:10

whatisnttaken
Scratcher
Registered: 2008-06-06
Posts: 5

Re: How do I make two sprites avoid each ohter?

Thanks for all the replies.

My general idea is to make two sprites avoid each other no matter the conditions. i.e. random motion, etc. I understand the principle of of "distance to" and "if touching" but it doesn't seem to work as it should in my simulation. I have tried several models. In several cases using a sprite as a "background" to highlight the path the other sprite would take. I ultimately, however, wish to create such a simulation without using one sprite as a background. I am simply unsure of what I am doing incorrectly. I believe I am over complicating things, espcially with the scripts of the "background sprites".

How do I upload my projects?

Offline

 

Board footer