# Objects move on X & Y and follow mouse movement

**URL:** https://forum.kirupa.com/t/objects-move-on-x-y-and-follow-mouse-movement/634122
**Category:** flash
**Created:** [January 8, 2016, 2:35am UTC](https://forum.kirupa.com/t/objects-move-on-x-y-and-follow-mouse-movement/634122 "2016-01-08T02:35:38Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![Peter\_Jamin\_Dabba](https://avatars.discourse-cdn.com/v4/letter/p/b4bc9f/32.png) [@Peter\_Jamin\_Dabba](https://forum.kirupa.com/u/Peter_Jamin_Dabba)
#### Post date: [January 8, 2016, 2:35am UTC](https://forum.kirupa.com/t/objects-move-on-x-y-and-follow-mouse-movement/634122/1 "2016-01-08T02:35:38Z")

</div>

I have this code to make some objects for field variable to move on the X axis, how would I get this to at the same time apply the same effect and easing on the y axis as well?

I tried to make it move on the y by adding the y where ever needed under a different event listener render2, but it overwrite how it worked on the x. So need these to both work together at the same time

WORKING CODE

```javascript
import com.greensock.TweenNano;
import com.greensock.easing.*;

//ease type
var easeType = Expo.easeOut;
//xmouse will hold x position of the mouse in relation to the center of the stage.
//assuming that the stage center's value is 0;
var xmouse:Number = 0;
// percentage of the xmouse position
var xPct:Number = 0;
// speed or durationl
var speed:Number = 2;

// add event listener based on mouse movement
stage.addEventListener(MouseEvent.MOUSE_MOVE, render);

function render(e:MouseEvent):void
{
	//if xmouse pos is greater than the center of the stage
	if( e.stageX > stage.stageWidth/2) {
		
		xmouse= -(e.stageX -stage.stageWidth) - stage.stageWidth/2; //-512
		
	}
	else 
	{
		xmouse = ((stage.stageWidth/2) - e.stageX); //512
		
	}
	
	
	xPct = Math.round ((xmouse/stage.stageWidth) * 200);
	
	var fieldXto:Number = ((xPct/100) * (field_mc.width - stage.stageWidth)/2) + stage.stageWidth/2;

	TweenNano.to(field_mc,speed, {x:fieldXto, ease:easeType});
	
	
}

```

---

<div class="post-metadata">

### Author: ![Peter\_Jamin\_Dabba](https://avatars.discourse-cdn.com/v4/letter/p/b4bc9f/32.png) [@Peter\_Jamin\_Dabba](https://forum.kirupa.com/u/Peter_Jamin_Dabba)
#### Post date: [January 8, 2016, 2:39am UTC](https://forum.kirupa.com/t/objects-move-on-x-y-and-follow-mouse-movement/634122/2 "2016-01-08T02:39:00Z")

</div>

Here is an example of what I created with 2 listeners and one applied to X and one Applied to Y. It only follows the Y

CODE HERE

```javascript
import com.greensock.TweenNano;
import com.greensock.easing.*;

//ease type
var easeType = Expo.easeOut;
//xmouse will hold x position of the mouse in relation to the center of the stage.
//assuming that the stage center's value is 0;
var xmouse:Number = 0;
var ymouse:Number = 0;
// percentage of the xmouse position
var xPct:Number = 0;
var yPct:Number = 0;
// speed or durationl
var speed:Number = 2;

// add event listener based on mouse movement
stage.addEventListener(MouseEvent.MOUSE_MOVE, render);

function render(e:MouseEvent):void
{
	//if xmouse pos is greater than the center of the stage
	if( e.stageX > stage.stageWidth/2) {
		
		xmouse= -(e.stageX -stage.stageWidth) - stage.stageWidth/2; //-512
		
	}
	else 
	{
		xmouse = ((stage.stageWidth/2) - e.stageX); //512
		
	}
	
	
	xPct = Math.round ((xmouse/stage.stageWidth) * 200);
	
	var fieldXto:Number = ((xPct/100) * (field_mc.width - stage.stageWidth)/2) + stage.stageWidth/2;

	TweenNano.to(field_mc,speed, {x:fieldXto, ease:easeType});
	
	
}

// add event listener based on mouse movement
stage.addEventListener(MouseEvent.MOUSE_MOVE, render2);

function render2(e:MouseEvent):void
{
	if( e.stageY > stage.stageWidth/2) {
		
		ymouse= -(e.stageY -stage.stageHeight) - stage.stageHeight/2; //-512
		
	}
	else 
	{
		ymouse = ((stage.stageHeight/2) - e.stageY); //512
		
	}
	
	
	yPct = Math.round ((ymouse/stage.stageHeight) * 200);
	
	var fieldYto:Number = ((yPct/100) * (field_mc.height - stage.stageHeight)/2) + stage.stageHeight/2;

	TweenNano.to(field_mc,speed, {y:fieldYto, ease:easeType});
	
	
}

```

---

<div class="post-metadata">

### Author: ![kirupa](https://yyz1.discourse-cdn.com/flex011/user_avatar/forum.kirupa.com/kirupa/32/11616_2.png) [@kirupa](https://forum.kirupa.com/u/kirupa)
#### Post date: [January 10, 2016, 11:51pm UTC](https://forum.kirupa.com/t/objects-move-on-x-y-and-follow-mouse-movement/634122/3 "2016-01-10T23:51:23Z")

</div>

While I realize this is entirely JavaScript, some of the snippets of code from here may help: [http://www.kirupa.com/canvas/mouse\_follow\_ease.htm](http://www.kirupa.com/canvas/mouse_follow_ease.htm)

Cheers,  
Kirupa 🙂

---

<div class="post-metadata">

### Author: ![Peter\_Jamin\_Dabba](https://avatars.discourse-cdn.com/v4/letter/p/b4bc9f/32.png) [@Peter\_Jamin\_Dabba](https://forum.kirupa.com/u/Peter_Jamin_Dabba)
#### Post date: [January 13, 2016, 4:20am UTC](https://forum.kirupa.com/t/objects-move-on-x-y-and-follow-mouse-movement/634122/4 "2016-01-13T04:20:17Z")

</div>

Thanks will check it out, yeah looking for something AS3, I guess they are similar
