# Limited Mouse Follow (please help)

**URL:** https://forum.kirupa.com/t/limited-mouse-follow-please-help/11939
**Category:** flash
**Created:** [January 24, 2003, 6:35am UTC](https://forum.kirupa.com/t/limited-mouse-follow-please-help/11939 "2003-01-24T06:35:34Z")
**Posts on this page:** 12
**Page:** 1

<div class="post-metadata">

### Author: ![Rogue\_Znowman](https://avatars.discourse-cdn.com/v4/letter/r/839c29/32.png) [@Rogue\_Znowman](https://forum.kirupa.com/u/Rogue_Znowman)
#### Post date: [January 24, 2003, 6:35am UTC](https://forum.kirupa.com/t/limited-mouse-follow-please-help/11939/1 "2003-01-24T06:35:34Z")

</div>

I’m relatively new to flash, and am trying to figure out a way for a character’s eyes to follow the curser.  
I’ve attached the swf of my character (A\*\*\*\*aka from Princess Mononoke).

I understand the simple mouse follow, but I don’t want his eyes leaving their sockets to chase after the cursor. :o  
Is there a way that I can set a boarder so that the eyes stay within a certain area, but are drawn (within that area) towards the cursor?

His eyes (together) are one symbol. I want them to both follow the mouse, but not go cross-eyed. Know what I mean?

Any help would be appreciated.

Thanks,

- Rogue Znowman

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 24, 2003, 7:15am UTC](https://forum.kirupa.com/t/limited-mouse-follow-please-help/11939/2 "2003-01-24T07:15:06Z")

</div>

There was something like this before…

[http://www.kirupaforum.com/showthread.php?s=&threadid=9125](http://www.kirupaforum.com/showthread.php?s=&threadid=9125)

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 24, 2003, 7:38am UTC](https://forum.kirupa.com/t/limited-mouse-follow-please-help/11939/3 "2003-01-24T07:38:26Z")

</div>

Thanks lostinbeta, sorry for not catching that before. =)

Hmm… actually, that’s not quite what I need. Eki demonstrated how to make an object rotate in the direction of the cursor, but that’s not the same as following it. I think he was onto it at first with his **if(eye.hitTest(eyeCavity))** suggestion, but I’m not sure how to implement that.

(If it only rotates, than it will never look “forward” again, only off towards the outside).

I think I’ll use easing combined with the function, but my first priority is to get the mouse-follow working. :hangover:

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 24, 2003, 7:44am UTC](https://forum.kirupa.com/t/limited-mouse-follow-please-help/11939/4 "2003-01-24T07:44:48Z")

</div>

Well you can use easing and just use if statements to tell it to stop moving, but that only allows you an invisible box, so the pupil of the eye may still get out of view.

Example…

```auto
if (this._x <= 200){
this._x = 200;
}

```

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 24, 2003, 7:54am UTC](https://forum.kirupa.com/t/limited-mouse-follow-please-help/11939/5 "2003-01-24T07:54:40Z")

</div>

That makes sense, I’ll give it a shot right now.

Until now I had this for my AS:

```auto

onClipEvent (load) {
	_x = 270;
	_y = 190;
	speed = 5;
}
onClipEvent (enterFrame) {
	endX = _root._xmouse;
	endY = _root._ymouse;
	_x += (endX-_x)/speed;
	_y += (endY-_y)/speed;

}

```

Which was a straight forward mouse follow with easing. Then I was trying to figure out how to incorporate **if(eye.hitTest(eyeCavity))**, but wasn’t quite understanding that line of code.

What you just said makes sense to me though, so I’ll try that first.

Thanks! :beam:

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 24, 2003, 8:03am UTC](https://forum.kirupa.com/t/limited-mouse-follow-please-help/11939/6 "2003-01-24T08:03:04Z")

</div>

Ok, with easing you will have to know the exact \_x and \_y coordinates of the center of your eye area.

So you can position it like (note these are made up coords)…

```auto
onClipEvent (load) {
centerX = 400
centerY = 300
_x = centerX;
_y = centerY;
speed = 5;
}

```

Then after position it originally and stuff you will have to define the enterFrame.

```auto
onClipEvent (load) {
_x = 0;
_y = 0;
speed = 5;
}
onClipEvent (enterFrame) {
endX = _root._xmouse;
endY = _root._ymouse;
_x += (endX-_x)/speed;
_y += (endY-_y)/speed;
if (_x <= centerX-20){
  _x = centerX-20;
} else if (_x >= centerX+20){
  _x = centerX+20;
}
if (_y <= centerY-20){
  _y = centerY-20;
} else if (_y >= centerY+20){
  _y = centerY+20
}
}

```

This is way untested but what it should do is…

Check the \_x and \_y coordinates compared to the centerX and centerY variables. If the \_x position is 20 pixels to the left or to the right of the center point it will stop there, and if the \_y position is 20 pixels above or below the centerpoint it will stop there.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 24, 2003, 8:29am UTC](https://forum.kirupa.com/t/limited-mouse-follow-please-help/11939/7 "2003-01-24T08:29:57Z")

</div>

Heh. I wish I would’ve seen this last post of yours sooner. 😛

I put together this messy piece of crap:

```auto

onClipEvent (load) {
	_x = 270;
	_y = 196;
	speed = 5;
}
onClipEvent (enterFrame) {
	if (this._x <= 275)
		this._x = 275;
	else {
		endX = _root._xmouse;
		_x += (endX-_x)/speed;
	}
	if (this._x >= 280)
		this._x = 280;
	else {
		endX = _root._xmouse;
		_x += (endX-_x)/speed;
	}
	if (this._y <= 195)
		this._y = 195;
	else {
		endY = _root._ymouse;
		_y += (endY-_y)/speed;
	}
	if (this._y >= 197)
		this._y = 197;
	else {
		endY = _root._ymouse;
		_y += (endY-_y)/speed;
	}
}

```

I had already set the initial position of the center.

```auto

	_x = 270;
	_y = 190;

```

It seems to only work for the bottom and right edges, and not the top or left edges, and I haven’t figured out why.

Here’s a link to the swf file:

[http://swl.netfirms.com/flash/eyetest.swf](http://swl.netfirms.com/flash/eyetest.swf)

Your coding is much more compact and efficient, so I’ll see if it works for me. :beam:

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 24, 2003, 8:44am UTC](https://forum.kirupa.com/t/limited-mouse-follow-please-help/11939/8 "2003-01-24T08:44:01Z")

</div>

Yup, yours works perfectly, and is a lot more efficient coding. :A+:  
(Except for forgetting a semicolon on line 19. :o )

So now I have the following:

```auto

onClipEvent (load) {
	centerX = 270
	centerY = 190
	_x = centerX;
	_y = centerY;
	speed = 25;
}

onClipEvent (enterFrame) {
	endX = _root._xmouse;
	endY = _root._ymouse;
	_x += (endX-_x)/speed;
	_y += (endY-_y)/speed;
	if (_x <= centerX-10)
		_x = centerX-10; 
	else if (_x >= centerX+10)
		_x = centerX+10;
	if (_y <= centerY-5)
		_y = centerY-5;
	else if (_y >= centerY+5)
		_y = centerY+5;
}

```

And here is the finished result:

[http://swl.netfirms.com/flash/eyetest2.swf](http://swl.netfirms.com/flash/eyetest2.swf)

* * *

Hey, thanks a lot for all your help! I’ve visited [Kirupa.com](http://Kirupa.com) often for tutorials and such, but this was my first time visiting the forum. I’m very impressed with the quick and accurate response/help. 😃

Chances are I’ll be visiting again soon. =)

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 24, 2003, 5:12pm UTC](https://forum.kirupa.com/t/limited-mouse-follow-please-help/11939/9 "2003-01-24T17:12:50Z")

</div>

Ooooppps, sorry about the no semi-colon in line 19…lol. I really can’t believe I did that, it is natural for me to add semi colons now. Hmmm, maybe because it was like 3am when I was helping you…lol.

That is a really cool result! I really wasn’t sure my code would work as I was just winging it and going by theory really. My code is usually wrong when I do that.

I am glad you enjoy it here at kirupaforums… I have been an addict from the day I signed up!!!

Ok… now just for a little note… you can use variables to set the distance. So this line centerX-10 could become centerX-distX and in the onClipEvent(load) you can have the variable distX = 10

It would make for easier updating if you ever have to use this code in the future.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 24, 2003, 7:48pm UTC](https://forum.kirupa.com/t/limited-mouse-follow-please-help/11939/10 "2003-01-24T19:48:21Z")

</div>

Heh. Don’t worry about the ; I was just buggin’ ya! 😛  
My prof used to haras us if we ever forgot to use 'em.

I’ve taken 4 years of University, mostly in Computer Science and Commerce, but it’s been almost 3 years since I’ve done any coding and I’m a little rusty.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 24, 2003, 8:24pm UTC](https://forum.kirupa.com/t/limited-mouse-follow-please-help/11939/11 "2003-01-24T20:24:49Z")

</div>

Yeah, I am sure if I went to school for it I would get in trouble a lot :P…lol… j/k. Actually I don’t usually forget semi colons, I got so used to needing them there in Javascript (learned that before AS) or I would get an error that now I tend to type them in without realizing it. PHP is really picky about them too, your whole script relies on 1 semi-colon, man I hate that…lol. I just started PHP and when I get fatal errors I hate scanning my script for one tiny mistake. At least Flash isn’t so picky, you can leave it out and it doesn’t care.

---

<div class="post-metadata">

### Author: ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)
#### Post date: [January 25, 2003, 10:58am UTC](https://forum.kirupa.com/t/limited-mouse-follow-please-help/11939/12 "2003-01-25T10:58:00Z")

</div>

Actionscript= it assumes things are there= great coding for those that code!!!

Peace
