A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Moving along a line

  1. #1
    Junior Member
    Join Date
    May 2008
    Posts
    18

    Moving along a line

    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.

    Thanks

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    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²)

    PHP Code:
    angleToBullseye:Number Math.atan2(bullseye.canon.ybullseye.canon.x);
    distanceToTravel:int 1000;

    destination:Point Point.polar(distanceToTravelangleToBullseye); 

  3. #3
    Junior Member
    Join Date
    May 2008
    Posts
    18
    I used your style and this is how it goes for me

    Code:
    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)});
    }
    Attached Images Attached Images

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center