[LEFT]Hi, i have created some code to move an object to a mouse pointer’s position. The problem is that it does not travel in a straight line.
I think the problem has to do with the conditions of x and y. I want to calculate the gradient(x/y or (x1-x2)/(y1-y20) or create an incrementation of a set of points that are similar to the gradient to reach the destination…i dont know how to do that tho.
var circle:Sprite = new Sprite;
circle.graphics.lineStyle(1,0x0000FF);
circle.graphics.drawCircle(100,100,50);
circle.graphics.endFill();
var mouseClickCheck:Boolean = false;
var circleX:Number = 0;
var circleY:Number = 0;
stage.addEventListener(MouseEvent.CLICK, handleMouseMove);
addEventListener(Event.ENTER_FRAME, UpdatePosition);
addChild(circle);
function UpdatePosition(event:Event):void
{
if(mouseClickCheck)
{
trace("[circleX] = " + circleX);
trace("[circle.x] = " + circle.x);
if(circle.x <= circleX)
{
circle.x += 100;
if(circle.x > circleX)
{
circle.x = circleX;
}
}
if(circle.x >= circleX)
{
circle.x -= 100;
}
if(circle.y <= circleY)
{
circle.y += 100;
if(circle.y > circleY)
{
circle.y = circleY;
}
}
if(circle.y >= circleY)
{
circle.y -= 100;
}
}
}
function handleMouseMove(event:MouseEvent):void
{
mouseClickCheck = true;
circleX = mouseX;
circleY = mouseY;
var g1:Sprite = new Sprite();
g1.graphics.lineStyle(2,0x0000FF);
g1.graphics.beginFill(0x0000FF);
g1.graphics.drawCircle(mouseX,mouseY,5);
addChild(g1);
}
[/LEFT]