Limited Mouse Follow (please help)

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

There was something like this before…

http://www.kirupaforum.com/showthread.php?s=&threadid=9125

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:

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…

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

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

Until now I had this for my AS:


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:

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)…

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.

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.

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

I put together this messy piece of crap:


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.


	_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

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

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:


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


Hey, thanks a lot for all your help! I’ve visited 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. :smiley:

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

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.

Heh. Don’t worry about the ; I was just buggin’ ya! :stuck_out_tongue:
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.

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.

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

Peace