4 Replies Latest reply on May 28, 2011 2:26 PM by prodigalmaster

# 360 degree movement

OK I have worked out the code and it is good:

```var xDist:Number;
var yDist:Number;
var xRatio:Number;
var yRatio:Number;
var xSpeed:Number;
var ySpeed:Number;
ball.onEnterFrame = function() {
xDist = _xmouse-this._x;
yDist = _ymouse-this._y;
xSpeed = xDist/20;
ySpeed = yDist/20;
this._x += xSpeed;
this._y += ySpeed;
};
```

However, due to the division of the xDist and yDist it makes the 'ball' slow down the nearer it gets to the mouse position, what I want is the speed the ball moves to the mouse stays constant but also stays 360 degree movement. I thought of working out the ratio of Xdist against yDist then muliplying that against a constantSpeed but... I couldnt get anything to work.

Thanks.

• ###### 1. Re: 360 degree movement

that won't make any sense.  what happens are the ball and mouse are at the same position?

• ###### 2. Re: 360 degree movement

it works. Notice the: /20;

if I used /2 instead and If the x distance is 100 then the speed would be 50 pixels then 25 pixels because the distance is now 50 then 12.5 pixels then 6.25 and so on until it got very slow.

• ###### 3. Re: 360 degree movement

your current code works but that's because it's easing in.

it doesn't make any sense to remove that ease because without it the mouse and ball will have exactly the same position after an initial movement of the ball.

• ###### 4. Re: 360 degree movement

Yeah and thats why I need help. How do I make the speed constant? Probably completelly seperate code. :/

-snip-

FINAL EDIT:

I got it to work! here is the code if anyone wants 360 degree constant(same speed) movement with no ease using the mouse.

```var xDist:Number;
var yDist:Number;
var xRatio:Number;
var yRatio:Number;
var xSpeed:Number;
var ySpeed:Number;
var baseSpeed:Number = 4;
ball.onEnterFrame = function() {
if (_xmouse<this._x) {
xDist = this._x-_xmouse;
} else{
xDist = _xmouse-this._x;
}
if (_ymouse<this._y) {
yDist = this._y-_ymouse;
} else {
yDist = _ymouse-this._y;
}
//Movement
if (yDist>0 && xDist>0 && yDist<xDist) {
yRatio = yDist/xDist;
xRatio = 1;
}
if (yDist>0 && xDist>0 && xDist<yDist) {
xRatio = xDist/yDist;
yRatio = 1;
}
//
if (yDist<0 && xDist<0 && yDist<xDist) {
yRatio = yDist/xDist;
xRatio = -1;
}
if (yDist<0 && xDist<0 && xDist<yDist) {
xRatio = xDist/yDist;
yRatio = -1;
}
if (xRatio>=1) {
xRatio = 1;
}
if (yRatio>=1) {
yRatio = 1;
}
if (xRatio<=-1) {
xRatio = -1;
}
if (yRatio<=-1) {
yRatio = -1;
}
xSpeed = xRatio*baseSpeed;
ySpeed = yRatio*baseSpeed;
trace(xRatio+" "+yRatio);
//
if (_xmouse<this._x) {
this._x -= xSpeed;
}
if (_ymouse<this._y) {
this._y -= ySpeed;
}
//
if (_xmouse>this._x) {
this._x += xSpeed;
}
if (_ymouse>this._y) {
this._y += ySpeed;
}
};
```