Search code examples
actionscript-3angletweener

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


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??


Solution

  • 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} );