5 Replies Latest reply on Feb 6, 2011 8:01 AM by gherome_gherome

    How to stop my game character from shooting every time he rotates...(?)

    gherome_gherome

      Greetings;

       

      I've just been working with AS3 for two weeks (before that I had only made simple animations from within an FLA). Anyway, I have the very beginnings of a game I'm working on..a stick figure that moves via the arrow keys, shoots via the space bar, and rotates via the A and D keys. The problem is that he unintentionally shoots whenever I rotate using  the A and D keys. I think it's because the x and y velocities imparted to the 'missles' are functions (sin/cos) of the player.rotation.

       

      Is there another way to program this so he doesn't shoot when he rotates?

       

      Thanks,

       

      Jeremy

       

       

      SWF:

       

      http://thevue.com/jeremy/ideas/schoolOfFish.swf

       

       

      Main.as:

       

       

      package

      {

      import flash.display.MovieClip;

      import flash.events.MouseEvent;

      import flash.events.Event;

      import flash.geom.Rectangle;

      import flash.events.KeyboardEvent;

      import flash.ui.Keyboard;

       

       

       

      public class Main extends MovieClip

      {

      // declaring variables:

      var fishMov:FishMov;

      var fishMov1:FishMov;

      var fishMov2:FishMov;

      var ceiling:Ceiling;

      var player:Player;

       

      var missle1:Missle;

      var missle2:Missle;

      var missle3:Missle;

      var missle4:Missle;

       

       

       

      var missleStartPoint = MissleStartPoint;

      var vx = 0;

      var vy = 0;

      var ax = 0;

      var ay = 0;

      var vx1 = 0;

      var vy1 = 0;

      var ax1 = 0;

      var ay1 = 0;

      var vx2 = 0;

      var vy2 = 0;

      var ax2 = 0;

      var ay2 = 0;

      var playervx = 0;

      var playervy = 0;

      var playeray = 0;

       

       

      var missle1vx = 0;

      var missle1vy = 0;

      var missle2vx = 0;

      var missle2vy = 0;

      var missle3vx = 0;

      var missle3vy = 0;

      var missle4vx = 0;

      var missle4vy = 0;

       

       

       

       

       

      var missleCounter = 1;

      //

       

       

       

       

      //running the program using Main.as

      public function Main()

      {

      //attaching movieclip classes to the instances:

       

      fishMov = new FishMov;

      fishMov1 = new FishMov;

      fishMov2 = new FishMov;

      ceiling = new Ceiling;

      player = new Player;

      missle1 = new Missle;

      missle2 = new Missle;

      missle3 = new Missle;

      missle4 = new Missle;

       

      //adding instances to the display list:

       

      for (var i=1; i<5; i++)

      {

      addChild(this["missle"+i]);

      this["missle"+i].x = -100;

      this["missle"+i].y = -100;

      this["missle"+i].height = 30;

      this["missle"+i].width = 30;

      }

       

       

       

       

      addChild(fishMov);

      fishMov.x = stage.stageWidth/2-100;

          fishMov.y = stage.stageHeight/2-120;

      fishMov.width = 999/10;

      fishMov.height = 700/10;

      addChild(fishMov1);

      fishMov1.x = stage.stageWidth/2-100-150;

          fishMov1.y = stage.stageHeight/2-120+150;

      fishMov1.width = 999/10;

      fishMov1.height = 700/10

      addChild(fishMov2);

      fishMov2.x = stage.stageWidth/2-100+150;

          fishMov2.y = stage.stageHeight/2-120+300;

      fishMov2.width = 999/10;

      fishMov2.height = 700/10

      addChild(ceiling);

      ceiling.width = stage.stageWidth;

      addChild(player);

      player.x = 50;

      player.y = stage.stageHeight/2+300;

      player.height = 50;

      player.width = 80;

      player.rotation = 0;

       

      //function defintition for missleDisappear():

       

      function missleReset():void

      {

       

      missle1vx = 0;

      missle1vy = 0;

      missle2vx = 0;

      missle2vy= 0;

      missle3vx = 0;

      missle3vy = 0;

      missle4vx = 0;

      missle4vy= 0;

      }

       

      //function defintition for missleLaunch():

       

      function missleLaunch():void

      {

      if (missleCounter == 5)

      { missleCounter = 1;}

      var missleCosFactor = Math.cos((player.rotation/180)*Math.PI);

      var missleSinFactor = Math.sin((player.rotation/180)*Math.PI);

       

      if (missleCounter == 1)

      {

      missle1.x = player.x;

      missle1.y = player.y;

      missle1.alpha = 100;

      missle1vx = 20*missleCosFactor;

      missle1vy = 20*missleSinFactor;

       

      }

      if (missleCounter == 2)

      {

      missle2.x = player.x;

      missle2.y = player.y;

      missle2.alpha = 100;

      missle2vx = 20*missleCosFactor;

      missle2vy = 20*missleSinFactor;

       

      }

      if (missleCounter == 3)

      {

      missle3.x = player.x;

      missle3.y = player.y;

      missle3.alpha = 100;

      missle3vx = 20*missleCosFactor;

      missle3vy = 20*missleSinFactor;

       

      }

      if (missleCounter == 4)

      {

      missle4.x = player.x;

      missle4.y = player.y;

      missle4.alpha = 100;

      missle4vx = 20*missleCosFactor;

      missle4vy = 20*missleSinFactor;

       

      }

      }

       

       

      addEventListener(Event.ENTER_FRAME, onEnterFrame)

      function onEnterFrame(event:Event):void

       

      {

      missle1.x += missle1vx;

      missle1.y += missle1vy;

      missle2.x += missle2vx;

      missle2.y += missle2vy;

      missle3.x += missle3vx;

      missle3.y += missle3vy;

      missle4.x += missle4vx;

      missle4.y += missle4vy;

       

       

      // controlling speed in x:

      player.x += playervx;

      if (playervx > 2)

      { playervx = 2;}

      if (playervx < -2)

      { playervx = -2;}

      // controlling speed in y:

      player.y += playervy;

      if (playervy > 2)

      { playervy = 2;}

      if (playervy < -2)

      { playervy = -2;}

      // friction in x & y:

       

      playervx += -0.02*playervx;

      playervy += -0.02*playervy;

       

      fishMov.fish.y += vy;

      fishMov1.fish.y += vy1;

      fishMov2.fish.y += vy2;

      vx += ax;

      vy += ay;

      vx1 += ax1;

      vy1 += ay1;

      vx2 += ax2;

      vy2 += ay2;

      playervy += playeray;

       

      if (fishMov.fish.hitTestObject(ceiling))

      {  

      fishMov.fish.y += ceiling.height;

      }

       

      if (missle1.hitTestObject(ceiling))

      {  

      missle1vy = -missle1vy;

      }

      if (missle2.hitTestObject(ceiling))

      {  

      missle2vy = -missle2vy;

      }

      if (missle3.hitTestObject(ceiling))

      {  

      missle3vy = -missle3vy;

      }

      if (missle4.hitTestObject(ceiling))

      {  

      missle4vy = -missle4vy;

      }

       

       

      if (missle1.missleCircle.hitTestObject(fishMov.fish))

      {  

      fishMov.fish.fish_gr.gotoAndStop(2);

      missle1.x = missle1.y = -100;

      missle1.alpha = 0;

      ay += 5;

      vx = vy = 10;

      }

      if (missle2.missleCircle.hitTestObject(fishMov.fish))

      {  

      fishMov.fish.fish_gr.gotoAndStop(2);

      missle2.x = missle2.y = -100;

      missle2.alpha = 0;

      ay += 5;

      vx = vy = 10;

      }

      if (missle3.missleCircle.hitTestObject(fishMov.fish))

      {  

      fishMov.fish.fish_gr.gotoAndStop(2);

      missle3.x = missle3.y = -100;

      missle3.alpha = 0;

      ay += 5;

      vx = vy = 10;

      }

      if (missle4.missleCircle.hitTestObject(fishMov.fish))

      {  

      fishMov.fish.fish_gr.gotoAndStop(2);

      missle4.x = missle4.y = -100;

      missle4.alpha = 0;

      ay += 5;

      vx = vy = 10;

      }

       

      if (missle1.missleCircle.hitTestObject(fishMov1.fish))

      {  

      fishMov1.fish.fish_gr.gotoAndStop(2);

      missle1.x = missle1.y = -100;

      missle1.alpha = 0;

      ay1 += 5;

      vx1 = vy1 = 10;

      }

      if (missle2.missleCircle.hitTestObject(fishMov1.fish))

      {  

      fishMov1.fish.fish_gr.gotoAndStop(2);

      missle2.x = missle2.y = -100;

      missle2.alpha = 0;

      ay1 += 5;

      vx1 = vy1 = 10;

      }

      if (missle3.missleCircle.hitTestObject(fishMov1.fish))

      {  

      fishMov1.fish.fish_gr.gotoAndStop(2);

      missle3.x = missle3.y = -100;

      missle3.alpha = 0;

      ay1 += 5;

      vx1= vy1 = 10;

      }

      if (missle4.missleCircle.hitTestObject(fishMov1.fish))

      {  

      fishMov1.fish.fish_gr.gotoAndStop(2);

      missle4.x = missle4.y = -100;

      missle4.alpha = 0;

      ay1 += 5;

      vx1 = vy1 = 10;

      }

      if (missle1.missleCircle.hitTestObject(fishMov2.fish))

      {  

      fishMov2.fish.fish_gr.gotoAndStop(2);

      missle1.x = missle1.y = -100;

      missle1.alpha = 0;

      ay2 += 5;

      vx2 = vy2 = 10;

      }

      if (missle2.missleCircle.hitTestObject(fishMov2.fish))

      {  

      fishMov2.fish.fish_gr.gotoAndStop(2);

      missle2.x = missle2.y = -100;

      missle2.alpha = 0;

      ay2 += 5;

      vx2 = vy2 = 10;

      }

      if (missle3.missleCircle.hitTestObject(fishMov2.fish))

      {  

      fishMov2.fish.fish_gr.gotoAndStop(2);

      missle3.x = missle3.y = -100;

      missle3.alpha = 0;

      ay2 += 5;

      vx2 = vy2 = 10;

      }

      if (missle4.missleCircle.hitTestObject(fishMov2.fish))

      {  

      fishMov2.fish.fish_gr.gotoAndStop(2);

      missle4.x = missle4.y = -100;

      missle4.alpha = 0;

      ay2 += 5;

      vx2 = vy2 = 10;

      }

       

       

      if (fishMov.fish.hitTestObject(player))

      {

      playeray += 2.2;

      player.gotoAndStop(2);

      }

      if (fishMov1.fish.hitTestObject(player))

      {

      playeray += 2.2;

      player.gotoAndStop(2);

      }

      if (fishMov2.fish.hitTestObject(player))

      {

      playeray += 2.2;

      player.gotoAndStop(2);

      }

      }

       

       

      stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPressed);

      function onKeyPressed(evt:KeyboardEvent):void

      {

      switch (evt.keyCode)

      {

      case Keyboard.LEFT:

      playervx -= 1;

      break;

      case Keyboard.RIGHT:

      playervx += 1;

      break;

      case Keyboard.UP:

      playervy -= 0.7;

      break;

      case Keyboard.DOWN:

      playervy += 0.7;

      break;

      case Keyboard.A:

      player.rotation -= 26;

       

       

      case Keyboard.D:

      player.rotation += 13

       

       

      case Keyboard.SPACE:

      missleLaunch();

      missleCounter += 1;

      trace(missleCounter);

       

      break;

       

      default:

       

      trace("keyCode:", evt.keyCode);

       

       

       

       

      }

       

      }

      }

       

      }

      }

        • 1. Re: How to stop my game character from shooting every time he rotates...(?)
          Ned Murphy Adobe Community Professional & MVP

          You'll probably help yourself if you can whittle that down to only the relevant code that involves initiating shooting.  Folks (like me) are less likely to try to wade thru it all looking for it--I gave it a quick once over and didn't see anything obvious that told me where the firing occurs.. no comments to help either

          1 person found this helpful
          • 2. Re: How to stop my game character from shooting every time he rotates...(?)
            gherome_gherome Level 1

            Ned,

             

             

            Point taken, I could definitely whittle down the code and comment it better, and I certainly will. As I said, I am very new to this. The part of the program dealing with the shooting is fairly simple though:

             

            1) I declare the missles as  instances of my Missle movieclip:

             

            var missle1:Missle;

            var missle2:Missle;

            var missle3:Missle;

            var missle4:Missle;

             

            2) I declare the variables which will be the x and y velocities of the missles:

             

            var missle1vx = 0;

            var missle1vy = 0;

            var missle2vx = 0;

            var missle2vy = 0;

            var missle3vx = 0;

            var missle3vy = 0;

            var missle4vx = 0;

            var missle4vy = 0;

             

            3) I declare a missleCounter variable that will later be used by the function that launches the missles:

             

            var missleCounter = 1;

             

             

            4) I add the missles, offstage and out of view:

             

             

            for (var i=1; i<5; i++)

            {

            addChild(this["missle"+i]);

            this["missle"+i].x = -100;

            this["missle"+i].y = -100;

            this["missle"+i].height = 30;

            this["missle"+i].width = 30;

            }

             

             

            5) I create a function to handle launching the missles and declare within it the two variables that determine the missle launch direction ('missleCosFactor' and 'missleSinFactor':

             

             

             

            function missleLaunch():void

            {

            if (missleCounter == 5)

            { missleCounter = 1;}

            var missleCosFactor = Math.cos((player.rotation/180)*Math.PI);

            var missleSinFactor = Math.sin((player.rotation/180)*Math.PI);

             

            if (missleCounter == 1)

            {

            missle1.x = player.x;

            missle1.y = player.y;

            missle1.alpha = 100;

            missle1vx = 20*missleCosFactor;

            missle1vy = 20*missleSinFactor;

             

            }

            if (missleCounter == 2)

            {

            missle2.x = player.x;

            missle2.y = player.y;

            missle2.alpha = 100;

            missle2vx = 20*missleCosFactor;

            missle2vy = 20*missleSinFactor;

             

            }

            if (missleCounter == 3)

            {

            missle3.x = player.x;

            missle3.y = player.y;

            missle3.alpha = 100;

            missle3vx = 20*missleCosFactor;

            missle3vy = 20*missleSinFactor;

             

            }

            if (missleCounter == 4)

            {

            missle4.x = player.x;

            missle4.y = player.y;

            missle4.alpha = 100;

            missle4vx = 20*missleCosFactor;

            missle4vy = 20*missleSinFactor;

             

            }

            }

             

             

             

            6) I add the missle velocities to the missle positions on every frame:

             

            addEventListener(Event.ENTER_FRAME, onEnterFrame)

            function onEnterFrame(event:Event):void

             

            {

            missle1.x += missle1vx;

            missle1.y += missle1vy;

            missle2.x += missle2vx;

            missle2.y += missle2vy;

            missle3.x += missle3vx;

            missle3.y += missle3vy;

            missle4.x += missle4vx;

            missle4.y += missle4vy;

            }

             

            7) I use the SPACE key to activate the missleLaunch() function and to increment the missleCounter variable it uses:

             

            case Keyboard.SPACE:

            missleLaunch();

            missleCounter += 1;

            trace(missleCounter);

             

             

            8) I use the A and D keys to rotate the player, but this also unintentionally causes the missles to launch:

             

            case Keyboard.A:

            player.rotation -= 26;

             

             

            case Keyboard.D:

            player.rotation += 13

             

             

             

            I realize that my code is confusing, so thanks for reading it and thanks in advance to anyone else who takes the time.

             

            - Jeremy

             

            • 3. Re: How to stop my game character from shooting every time he rotates...(?)
              Ned Murphy Adobe Community Professional & MVP

              You overdid it again, you only need to show the code relevant to launching a missile, which is whatever calls the missleLaunch() function.  You'll get the hang of limiting what you need to show eventually.

               

              The launch is initiated in the switch code in your onKeyPressed() function.  If you look at the switch case code for your rotation controls, you'll find something missing in each of those two cases that is not missing in any of the other cases.

              • 4. Re: How to stop my game character from shooting every time he rotates...(?)
                gherome_gherome Level 1

                Ned;

                 

                Thanks for helping me find the two missing break; statements in my switch code. I feel silly. This also explains why I had to rotate 26 degrees in one direction and 13 in the other   I'll try to be more brief next time.

                 

                Jeremy

                • 5. Re: How to stop my game character from shooting every time he rotates...(?)
                  Ned Murphy Adobe Community Professional & MVP

                  You're welcome Jeremy.

                   

                  It's not so much being brief as it is limiting what is offered to info that is immediately relevant to the problem--but brief can help initiate interest.  And with my having ranted on that... don't be too aggravated if the next time you post, someone says they need to see more... it can be a moving target.  But as far as first impressions go, showing relevant stuff only (intended functionality description, relevant code, and problem description) is a better start for luring in help.