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

#1 2011-06-24 13:55:47

samurai768
Scratcher
Registered: 2009-07-21
Posts: 1000+

How do I get something to follow the mouse within a certain area?

I'm making a top secret game, and I need something to follow the mouse within a certain radius of another object, and when the mouse moves outside that radius, it continues to follow, but without leaving it's designated area.

Sorry if that was confusing  hmm  I can't really think of how to explain it any other way  tongue

Here's the script I have so far:

Note: I have a sprite following the mouse-pointer with a variable called distance.

when green flag clicked
forever
if< [73] > [(distance of mouse-pointer)] >
go to mouse pointer
else
?

I can't think of what to put instead of the ?.

Thanks, and Scratch on!

Offline

 

#2 2011-06-24 13:59:07

Kileymeister
Scratcher
Registered: 2008-04-17
Posts: 1000+

Re: How do I get something to follow the mouse within a certain area?

You could put in:

go to (other object)
point towards mouse pointer
move (73) steps

And if you want the object to always be facing the right way, just do:

go to (other object)
point towards mouse pointer
move (73) steps
point in direction (90)

Last edited by Kileymeister (2011-06-24 14:00:30)


I'm back, and showcasing two new* projects!  Click left or right on the image below to see!
http://img109.imageshack.us/img109/7905/part1l.pnghttp://img859.imageshack.us/img859/6417/part2bf.png

Offline

 

#3 2011-06-24 14:00:33

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

Re: How do I get something to follow the mouse within a certain area?

You could have it go to the edge of it's following radius at the same angle as the mouse is from the center. Hold on and I'll figure out a script.  smile


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

Offline

 

#4 2011-06-24 14:05:53

samurai768
Scratcher
Registered: 2009-07-21
Posts: 1000+

Re: How do I get something to follow the mouse within a certain area?

Kileymeister wrote:

You could put in:

go to (other object)
point towards mouse pointer
move (73) steps

And if you want the object to always be facing the right way, just do:

go to (other object)
point towards mouse pointer
move (73) steps
point in direction (90)

Hmm... it's still doing the same thing.

@Harakou
Okay!

Offline

 

#5 2011-06-24 14:11:51

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

Re: How do I get something to follow the mouse within a certain area?

Oh, one thing: will this other object move or have a fixed position?


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

Offline

 

#6 2011-06-24 14:13:14

Kileymeister
Scratcher
Registered: 2008-04-17
Posts: 1000+

Re: How do I get something to follow the mouse within a certain area?

Really?  I tried it on my program and it does what I expected it to do, maybe I don't understand what you want accomplished.

You want this sprite to go to the mouse pointer when the mouse is a certain distance from a different sprite, right?  And when the mouse is too far away, the object circles around to face the mouse pointer, but stays within its maximum allowed distance from the stationary sprite, correct?
And the variable "distance of mouse pointer" is the distance from the stationary sprite to the mouse pointer, not the distance from the moving sprite to the mouse pointer, right?

Last edited by Kileymeister (2011-06-24 14:17:37)


I'm back, and showcasing two new* projects!  Click left or right on the image below to see!
http://img109.imageshack.us/img109/7905/part1l.pnghttp://img859.imageshack.us/img859/6417/part2bf.png

Offline

 

#7 2011-06-24 14:30:44

samurai768
Scratcher
Registered: 2009-07-21
Posts: 1000+

Re: How do I get something to follow the mouse within a certain area?

Kileymeister wrote:

Really?  I tried it on my program and it does what I expected it to do, maybe I don't understand what you want accomplished.

You want this sprite to go to the mouse pointer when the mouse is a certain distance from a different sprite, right?  And when the mouse is too far away, the object circles around to face the mouse pointer, but stays within its maximum allowed distance from the stationary sprite, correct?
And the variable "distance of mouse pointer" is the distance from the stationary sprite to the mouse pointer, not the distance from the moving sprite to the mouse pointer, right?

Yeah. I think it didn't work (and please forgive me for leaving this out, I thought it would not be important) because it should only follow when it is dragged. I used the if mouse down and mouse-pointer touching script to achieve that  hmm  Sorry  sad

Harakou wrote:

Oh, one thing: will this other object move or have a fixed position?

Fixed position  smile

Offline

 

#8 2011-06-24 14:34:42

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

Re: How do I get something to follow the mouse within a certain area?

samurai768 wrote:

Harakou wrote:

Oh, one thing: will this other object move or have a fixed position?

Fixed position  smile

Oh, well that makes things a bit simpler for me. What is the position going to be?


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

Offline

 

#9 2011-06-24 14:40:29

Kileymeister
Scratcher
Registered: 2008-04-17
Posts: 1000+

Re: How do I get something to follow the mouse within a certain area?

Does this work?
http://i56.tinypic.com/xmstud.gif
Edit:  This one is a bit better:
http://i55.tinypic.com/if1xk2.gif
This one works just as well, but also takes into account displacement between the mouse and sprite (so the sprite doesn't jerk to the mouse's x/y positions when you start dragging it).

Last edited by Kileymeister (2011-06-24 14:46:47)


I'm back, and showcasing two new* projects!  Click left or right on the image below to see!
http://img109.imageshack.us/img109/7905/part1l.pnghttp://img859.imageshack.us/img859/6417/part2bf.png

Offline

 

#10 2011-06-24 14:47:33

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

Re: How do I get something to follow the mouse within a certain area?

Kileymeister wrote:

Does this work?
http://i56.tinypic.com/xmstud.gif

I should think it does, but a more mathematical way would be less glitchy (e.g. if you need to sense something). That's what I'm trying to figure out, and I succeeded theoretically, except it's enormously glitchy.

Offline

 

#11 2011-06-24 14:51:28

samurai768
Scratcher
Registered: 2009-07-21
Posts: 1000+

Re: How do I get something to follow the mouse within a certain area?

Kileymeister wrote:

Does this work?
http://i56.tinypic.com/xmstud.gif
Edit:  This one is a bit better:
http://i55.tinypic.com/if1xk2.gif
This one works just as well, but also takes into account displacement between the mouse and sprite (so the sprite doesn't jerk to the mouse's x/y positions when you start dragging it).

That works really well! Thank you!

Offline

 

#12 2011-06-24 15:03:46

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

Re: How do I get something to follow the mouse within a certain area?

samurai768 wrote:

Kileymeister wrote:

Does this work?
http://i56.tinypic.com/xmstud.gif
Edit:  This one is a bit better:
http://i55.tinypic.com/if1xk2.gif
This one works just as well, but also takes into account displacement between the mouse and sprite (so the sprite doesn't jerk to the mouse's x/y positions when you start dragging it).

That works really well! Thank you!

Aw, just when I was almost done with mine. Oh well, it wasn't working right anyway.


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

Offline

 

#13 2011-06-24 16:14:12

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

Re: How do I get something to follow the mouse within a certain area?

Harakou wrote:

samurai768 wrote:

Kileymeister wrote:

Does this work?
http://i56.tinypic.com/xmstud.gif
Edit:  This one is a bit better:
http://i55.tinypic.com/if1xk2.gif
This one works just as well, but also takes into account displacement between the mouse and sprite (so the sprite doesn't jerk to the mouse's x/y positions when you start dragging it).

That works really well! Thank you!

Aw, just when I was almost done with mine. Oh well, it wasn't working right anyway.

Mine was  tongue  Oh well, I practiced some trigonometry.

Offline

 

#14 2011-06-24 17:07:58

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

Re: How do I get something to follow the mouse within a certain area?

Here we go! I've completed the proper version of the script! It's glitchless and is completely theoretical, so that the sprite is only moved once a frame.

http://dl.dropbox.com/u/11532575/forums/directionPlanets.gif

planet is the main sprite, and moon revolves around it (following the mouse). You can easily add a if mouse down to it.

Offline

 

Board footer