I’m trying to make an object move to where the user clicks. The object follows through but it does not move directly towards the point. Any guidance on how to make the ball move in a line towards the position will be much appreciated.
Here’s the working code so far :
package {
import flash.display.Sprite;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.events.Event;
public class Main1 extends MovieClip {
private var canvas:Sprite;
private var canvasWidth:int;
private var canvasHeight:int;
private var iNum:int = 100;
private var ball:Sprite;
private var orgX:int;
private var orgY:int;
private var newX:int;
private var newY:int;
public function Main1():void {
canvas = new Sprite();
addChild(canvas);
canvas.graphics.beginFill(0xFFFFCC);
canvasWidth = 550;
canvasHeight = 400;
canvas.graphics.drawRect(0, 0, canvasWidth, canvasHeight);
canvas.graphics.endFill();
canvas.addEventListener(MouseEvent.CLICK, onClick);
addEventListener(Event.ENTER_FRAME, onFrameEnter, false, 0, true);
ball = new Sprite();
addChild(ball);
ball.graphics.lineStyle();
ball.graphics.beginFill(0xFF0000);
ball.graphics.drawCircle(0, 0, 15);
ball.graphics.endFill();
ball.x = canvasWidth/2;
ball.y = canvasHeight/2;
orgX = ball.x;
orgY = ball.y;
newX = ball.x;
newY = ball.y; }
private function onFrameEnter(e:Event):void{
if(newX>ball.x)
ball.x += 1;
else if(newX < ball.x)
ball.x -= 1;
if(newY>ball.y)
ball.y += 1;
else if(newY < ball.y)
ball.y -= 1;
}
private function movePos():void {
//ball.x += 5;
newX = mouseX;
newY = mouseY;
}
private function onClick(event:MouseEvent):void {
orgX = ball.x;
orgY = ball.y;
movePos();
}
Thanks!