7 Replies - 1749 Views - Last Post: 16 July 2008 - 01:25 PM

#1 bevans9908   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 31-October 07

Setting mouse follow limits

Post icon  Posted 14 July 2008 - 01:05 PM

Hi, I'm trying to set up a mouse follow and have managed to get it to work using the code below. However, I need to know how to set up a right and bottom limit so the target objects don't follow the cursor off the stage. Thanks.


_root.onEnterFrame=function() {
	posx=_root._xmouse;
	posy=_root._ymouse;
	pct_txt._x=pct_txt._x+(8+posx-pct_txt._x)/10
	pct_txt._y=pct_txt._y+(15+posy-pct_txt._y)/10
	pct_sign._x=pct_sign._x+(50+posx-pct_sign._x)/10
	pct_sign._y=pct_sign._y+(15+posy-pct_sign._y)/10
	bubble_mc._x=bubble_mc._x+(posx-bubble_mc._x)/10
	bubble_mc._y=bubble_mc._y+(posy-bubble_mc._y)/10
}



Is This A Good Question/Topic? 0
  • +

Replies To: Setting mouse follow limits

#2 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1651
  • View blog
  • Posts: 8,523
  • Joined: 07-September 06

Re: Setting mouse follow limits

Posted 14 July 2008 - 04:32 PM

Just add an if else statement:

if(_root._xmouse+bubble_mc._x+bubble_mc._width<Stage.width){
  // POSITION THE MCs _X
  posx=_root._xmouse;
  pct_txt._x=pct_txt._x+(8+posx-pct_txt._x)/10;
  pct_sign._x=pct_sign._x+(50+posx-pct_sign._x)/10;
  bubble_mc._x=bubble_mc._x+(posx-bubble_mc._x)/10;
}
if(_root._ymouse+bubble_mc._y+bubble_mc._height<Stage.height){
  // POSITION THE MCs _Y
  posy=_root._ymouse;
  pct_txt._y=pct_txt._y+(15+posy-pct_txt._y)/10;
  pct_sign._y=pct_sign._y+(15+posy-pct_sign._y)/10;
  bubble_mc._y=bubble_mc._y+(posy-bubble_mc._y)/10;
}


That shoudl work, but I haven't tested it and yoyu may need to tweak it a bit to get your desired result.
Was This Post Helpful? 0
  • +
  • -

#3 bevans9908   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 31-October 07

Re: Setting mouse follow limits

Posted 15 July 2008 - 05:49 AM

Thanks for the help. The code you suggested kind of works, but like you said, it needs to be tweaked. The only problem for me is that I am completely new to coding and wouldn't know where to start. If you feel like providing additional help I would really appreciate it. If not, I completely understand. Thanks again for the help.
Was This Post Helpful? 0
  • +
  • -

#4 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1651
  • View blog
  • Posts: 8,523
  • Joined: 07-September 06

Re: Setting mouse follow limits

Posted 15 July 2008 - 11:07 AM

Okay, I am not quite sure how big things are so if you could post their widths and heights I could come up with something that more accurately placed them when following the mouse.
Was This Post Helpful? 0
  • +
  • -

#5 bevans9908   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 31-October 07

Re: Setting mouse follow limits

Posted 15 July 2008 - 11:41 AM

Certainly. Here are the sizes:

pct_txt w:45.1 h:42.9
pct_sign w:25 h:40.6
bubble_mc w:77.3 h:77.3
stage w:802 h:675

By the way, I'm using Flash CS3 with AS 2.0. Thanks again for your help. I do appreciate any help you can provide.
Was This Post Helpful? 0
  • +
  • -

#6 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1651
  • View blog
  • Posts: 8,523
  • Joined: 07-September 06

Re: Setting mouse follow limits

Posted 15 July 2008 - 12:59 PM

Okay, I think this will be a solution for you. Place all the movieclips into a single movieclip (this will require redirecting any AS that is currently pointed at them outside of them through one more level (for instance _root.pct_txt would be _root.MC_HOLDER.pct_txt)

and then place this code instead of your current mouse trial code:

//holder is just a variable pointer at the mc that is holding the pct_txt, pct_sign, and bubble_mc MCs
// make it like so: var holder = _root.HOLDER_MC;
	var offset:Array = new Array(10, 10);
	_root.onmousemove = function(){
		if(_root._xmouse+offset[0]+holder._width<Stage.width){
			  // POSITION THE MCs _X
			  posx=_root._xmouse;
			  holder._x = posx+offset[0]
		}
		if(_root._ymouse+offset[1]+holder._height<Stage.height){
			// POSITION THE MCs _Y
			posy=_root._ymouse;
			  holder._y = posy+offset[1];
		}
	}


offset is an array giving the coordinates that you want the HOLDER_MC to be from the mouse, it is currently set at 10, and 10 (x and y) but can easily be changed.

Hope that helps, and don't feel bad if you need some more of an explanation on how to make it work.

<edit>
Place the MCs exactly like you want them to look within HOLDER_MC that way they will stay looking the way you make them and move with ease.
</edit>

This post has been edited by BetaWar: 15 July 2008 - 01:00 PM

Was This Post Helpful? 0
  • +
  • -

#7 bevans9908   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 7
  • Joined: 31-October 07

Re: Setting mouse follow limits

Posted 16 July 2008 - 09:52 AM

Many, many thanks! The code you provided didn't give me quite the effect I was looking for, but it gave me a very good start in the right direction. I ended up using a combination or your code and my original code along with a stage size change (1024 wide with an exact fit publish) and it works great! Thanks, again. You have been extremely helpful and patient!

Here is the code I used, in case there's someone else out there as lost as I was.

_root.onEnterFrame=function() {
		if(_root._xmouse<Stage.width){
			  posx=_root._xmouse;
			  posy=_root._ymouse;
			  holder._x = holder._x+(posx-holder._x)/15
			  holder._y = holder._y+(posy-holder._y)/15;
		}
}


Was This Post Helpful? 0
  • +
  • -

#8 BetaWar   User is offline

  • #include "soul.h"
  • member icon

Reputation: 1651
  • View blog
  • Posts: 8,523
  • Joined: 07-September 06

Re: Setting mouse follow limits

Posted 16 July 2008 - 01:25 PM

No problem, just glad to have been able to help.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1