Skip to main content
Known Participant
August 26, 2011
Question

Start game with space bar

  • August 26, 2011
  • 1 reply
  • 549 views

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

}

This topic has been closed for replies.

1 reply

Inspiring
August 26, 2011

try

keyListener = new Object();

keyListener.onKeyDown=function(){

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

      _root.playMenu.playButton.onPress();

   }

}

Key.addListener(keyListener);

Known Participant
August 26, 2011

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

Inspiring
August 26, 2011

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

}