AS3 move object towards x,y and then continue in the same direction (using tweener)

684 Views Asked by At

I'm building a simple space shooter game

using Tweener class I wrote a code that fires a bullet from the hero spaceship current position to the mouse current position

var fire = new Bullet();
        addChild(fire);
        fire.x = ship.x + (ship.width * 0.5);
        fire.y = ship.y
        Tweener.addTween(fire, {x:_me.currentTarget.mouseX, y:_me.currentTarget.mouseY, time: 5} );

the problem is this code makes the bullet stop at the last mouse position and I want the bullet to continue moving in the same direction until it's outside of the stage.

theoretically, the most simple way would be to input x.y position of the mouse as if it was at the same angle but outside of the stage but how can i get those x,y coordinates??

2

There are 2 best solutions below

8
Pan On BEST ANSWER

I think what you you want is to find a point one the same straight line with (fire.x, fire.y) , (_me.currentTarget.mouseX, _me.currentTarget.mouseY). And the point's x would be stage width.

So assume target point is A(stage.width, targetY)

We get

(targetY - mouseY)/(targetX - mouseX) = (mouseY - fire.y)/(mouseX - fire.x)

So

targetY = (mouseY - fire.y)/(mouseX - fire.x)*(targetX - mouseX) + mouseY;

Hero mouseX is _me.currentTarget.mouseX, mouseY is _me.currentTarget.mouseY

You can set targetX = stage.width + 10, then targetY

So you can get targetY, and do another tween

Tweener.addTween(fire, {x:targetX, y:targetY , time: 5} );

9
Discipol On

determine the angle of the bullet. using the angle, consider the origin of the bullet the center of a circle and the first point as a coordinate at the edge of the circle.

for the bullet to follow the same path, its just a larger circle, so the radius will increase.

the angle will be the same and so will the origin.