I would like to make a shooter which fires out of a canon at various points based on mouse click. So for now what i have done is I have a start point which is the mouth of the canon and the end point which is the place where the person clicks. And upon click, the cannon fires.
Simply put the canon waits for a mouse click. on click, it fires but the movement ends at point where the user clicked. This doesnt look cool if the target has moved or the user has not even clicked on the appropriate target. I would prefer if the cannonball could continue to move along that line until it goes off the stage when it is deleted.
Currently, I am using tweener class, so my code looks something like this.
-----------------
private function moveIt(e:Event):void{
Tweener.addTween(e.target,{x:mouseX,y:mouseY,time: 1,onComplete:disappearMe(e.target)});
}
private function disappearMe(e:Object):void {
Tweener.addTween(e,{delay:1,alpha:0});
}
-----------------
Any assistance would be greatly appreciated.
Calculate the angle and then extend it out so your bullet ends off stage...1000px should be enough but if your movie is huge, you can use the Pythagorean theorem to get the max distance: √(stageWidth² + stageHeight²)
private function moveIt(e:Event):void {
//Tweener.addTween(e.target,{x:mouseX,y:mouseY,time:1,onComplete:disappearMe(e.target)});
var angleToShoot:Number = Math.atan2(mouseY - robarm.y, mouseX - robarm.x);
var destinationTravel:int = 500;
var destination:Point = Point.polar(destinationTravel, angleToShoot);
Tweener.addTween(e.target,{x:destination.x,y:destination.y,time:3,onComplete:disappearMe(e.target)});
}
It fires at a super fast speed and i dont need it to go so far...just to clean it up, im going to try and use a while loop instead of the Tweener.addTween engine.
One thing i did notice is that I find the firing not quite to my mouse direction any more.
Check out Before using
Code:
private function moveIt(e:Event):void {
Tweener.addTween(e.target,{x:mouseX,y:mouseY,time:1,onComplete:disappearMe(e.target)});
}
And After using
Code:
private function moveIt(e:Event):void {
var angleToShoot:Number = Math.atan2(mouseY - robarm.y, mouseX - robarm.x);
var destinationTravel:int = 500;
var destination:Point = Point.polar(destinationTravel, angleToShoot);
Tweener.addTween(e.target,{x:destination.x,y:destination.y,time:3,onComplete:disappearMe(e.target)});
}