Skip to main content
Participant
December 17, 2012
Question

ball trajectory

  • December 17, 2012
  • 1 reply
  • 478 views

Hi,


I am trying to make a game that shoots a ball in a trajectory towards the mouse click. The longer you hold the mouse button the harder it shoots. But we cant get it to work correctly. The ball stops at the mouse click and then activates the gravity. Can anyone help me with this?

Here's the code:


package

{

          import flash.display.MovieClip;

          import flash.events.Event;

          import flash.utils.getTimer;

          import flash.events.MouseEvent;

          import fl.transitions.Tween;

          import fl.transitions.easing.None;

          public class Projectile extends MovieClip

          {

                    var gravity = 10;

                    var bounce = 0.55;

                    var startingspeed = 0;

                    var time;

                    var speedhorizontal = 0;

                    var speedvertical;

                    var score = 0;

                    var initialY;

                    var initialX;

                    static public var balls = 2;

                    var isMoving = false;

                    var mouseClicked = false;

                    var mouse = false;

                    var timenow;

                    var speedy;

                    public function Projectile()

                    {//Constructor

                              initialY = this.y;

                              initialX = this.x;

                              stage.addEventListener(MouseEvent.MOUSE_UP, playProjectile);

                              addEventListener(Event.ENTER_FRAME, animateProjectile);

                              addEventListener(Event.ENTER_FRAME,reset);

                              stage.addEventListener( MouseEvent.MOUSE_DOWN, mouseIsDown );

                              stage.addEventListener(MouseEvent.MOUSE_UP, mouseIsUp);

                              addEventListener(Event.ENTER_FRAME, endGame);

                    }

                    public function mouseIsDown(e:MouseEvent)

                    {

                              addEventListener(Event.ENTER_FRAME, checkMouseDown);

                    }

                    public function checkMouseDown(e:Event)

                    {

                              if (mouseClicked == false)

                              {

                                        speedhorizontal +=  1 / 2;

                              }

                              trace(speedhorizontal);

                    }

                    public function mouseIsUp(e:MouseEvent)

                    {

                              if (mouseClicked == false)

                              {

                                        time = getTimer();

                                        time = time / 100;

                                        new Tween (this, 'y', None.easeOut, this.y, stage.mouseY, 1, true);

                                        new Tween (this, 'x', None.easeOut, this.x, stage.mouseX, 1, true);

                                        mouseClicked = true;

                                        removeEventListener(Event.ENTER_FRAME, checkMouseDown);

                                        stage.addEventListener(Event.ENTER_FRAME, checkMouseUp);

                                        stage.addEventListener(Event.ENTER_FRAME, speed);

                              }

                    }

                    public function checkMouseUp(e:Event)

                    {

                              if (MovieClip(root).heero.currentFrame == 24)

                              {

                                        this.x +=  speedhorizontal;

                              }

                    }

                    public function speed(e:Event)

                    {

                              if (MovieClip(root).heero.currentFrame == 24)

                              {

                                        timenow = getTimer() / 100;

                                        speedy = gravity * (timenow - time) + startingspeed;

                                        this.y +=  speedy / 5;

                                        if (this.y > stage.stageHeight - (this.height / 2))

                                        {

                                                  this.y = stage.stageHeight - (this.height / 2);

                                                  speedy = speedy *  -  bounce;

                                                  time = getTimer() / 100;

                                                  startingspeed = speedy;

                                        }

                              }

                    }

                    public function reset(event:Event)

                    {

                              if (this.x >= stage.stageWidth + (this.width / 2))

                              {

                                        this.y = initialY;

                                        this.x = initialX;

                                        balls -=  1;

                                        isMoving = false;

                                        MovieClip(root).heero.gotoAndStop(0);

                                        speedhorizontal = 0;

                                        mouseClicked = false;

                                        startingspeed = 0;

                                        stage.removeEventListener(Event.ENTER_FRAME, speed);

                                        stage.removeEventListener(Event.ENTER_FRAME, checkMouseUp);

                              }

                    }

                    public function animateProjectile(event:Event)

                    {

                              if (isMoving && MovieClip(root).heero.currentFrame == 24)

                              {

                                        play();

                              }

                              else

                              {

                                        stop();

                              }

                    }

                    public function playProjectile(event:MouseEvent)

                    {

                              isMoving = true;

                    }

 

                    public function endGame (event:Event)

                    {

                              if (balls <= 0)

                              {

                                        stop()

                                        removeEventListener(Event.ENTER_FRAME, reset);

                                        removeEventListener(Event.ENTER_FRAME,reset);

                                        stage.removeEventListener(Event.ENTER_FRAME,speed);

                                        stage.removeEventListener(Event.ENTER_FRAME, checkMouseUp);

                                        removeEventListener(Event.ENTER_FRAME, checkMouseDown);

                              }

                    }

          }

}

thanks in advance

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
December 17, 2012

you should be using something like:

stage.addEventListener(MouseEvent.MOUSE_DOWN,downF);

stage.addEventListener(MouseEvent.MOUSE_UP,upF);

function downF(e:MouseEvent):void{

    powerStart=getTimer();

}

function upF(e:MouseEvent):void{

    angle = Math.atan2(mouseY-projectile.y,mouseX-projectile.x);

    power = (getTimer()-powerStart)/50;

    gravityStart = getTimer();

    if(mouseX>projectile.x){

        xVel = Math.cos(angle)*power;

    } else {

        xVel = -Math.cos(angle)*power;

    }

    yVel = Math.sin(angle)*power

    this.addEventListener(Event.ENTER_FRAME,shootF);

}

function shootF(e:Event){

    this.x += xVel;

    this.y += yVel*3 + (getTimer()-gravityStart)/100;  // pick constants that work for your situation

    if(this.y>stage.stageHeight||projectile.x>stage.stageWidth){

        this.removeEventListener(Event.ENTER_FRAME,shootF);

       this.x = initialX;

      this.y = initialY;

    }

}