3 Replies Latest reply on Aug 26, 2011 9:40 AM by Ron Colmen

    Start game with space bar

    browntimmy Level 1

      My project is almost complete except for this last little thing that's been bugging me. A tutorial I followed had you start the game by clicking a button but I would like to change that to pressing the spacebar starts the game. I tried:

       

      if(Key.isDown)(Key.SPACE))

      {

           _root.ship.newGame();

      }

       

      But nothing happens when I press the spacebar.

       

      Here's the starting code with the button for my file Ship.as

       

      function onLoad()
      {
           _visible = false;
           _root.winMenu._visible = false;
           _root.gameOverMenu._visible = false;
           _root.healthMeter._visible = false;
           _root.enemyHealthMeter._visible = false;
      
           _root.playMenu.playButton.onPress = function()
           {
                _root.ship.newGame();
           }
      }
      
      function newGame()
      {
      //code for the main game screen
      }
      
      

        • 1. Re: Start game with space bar
          Ron Colmen Level 2

          try

           

           

          keyListener = new Object();

          keyListener.onKeyDown=function(){

             if(Key.isDown(Key.SPACE)){

                _root.playMenu.playButton.onPress();

             }

          }

          Key.addListener(keyListener);

          1 person found this helpful
          • 2. Re: Start game with space bar
            browntimmy Level 1

            Edit: Okay that works, as long as I keep the button code and move the button off stage.

            • 3. Re: Start game with space bar
              Ron Colmen Level 2

              try

               

              on Frame 1

              stop();

              keyListener = new Object();

              keyListener.onKeyDown=function(){

                 if(Key.isDown(Key.SPACE)){

                    gotoAndStop(2) //assuming your function onLoad() is in frame 2

                 }

              }

              Key.addListener(keyListener);

               

               

              on Frame 2

              function onLoad()

              {

                   _visible = false;

                   _root.winMenu._visible = false;

                   _root.gameOverMenu._visible = false;

                   _root.healthMeter._visible = false;

                   _root.enemyHealthMeter._visible = false;

               

                   _root.ship.newGame();

              }

              function newGame()

              {

              //code for the main game screen

              }