By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,642 Members | 1,433 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,642 IT Pros & Developers. It's quick & easy.

go inside if statment onces every time.

P: 59
in main. when user press space bar set 'space' to true.
if user let go of space bar set 'space' to false.

Expand|Select|Wrap|Line Numbers
  1. boolean space
  2. boolean shooting = false;
  3.  
  4. if(space && !shooting)
  5.         {    
  6.             //shoot bullets
  7.             shooting = true;
  8.         }
  9.         if(!space)
  10.         {
  11.             shooting = false;
  12.         }

when user hold space bar than it will only go inside this if statment onces. so it will shoot one bullet. if user want to shoot another bullet than they have to press the space bar again.

is their way to set same effect by only using space variable?

i tried this code below but it doesnt really work. bc this is in game loop so it will keep going in side it.
Expand|Select|Wrap|Line Numbers
  1. boolean space
  2. boolean shooting = false;
  3.  
  4. if(space)
  5.         {    
  6.             //shoot bullets
  7.         m.setSpace(false);
  8.  
Apr 13 '13 #1
Share this Question
Share on Google+
1 Reply


10K+
P: 13,264
You'll need to listen for keyPressed and keyReleased separately. In keyPressed start a loop that depends on a variable that is accessible in keyReleased so that you can stop the keyPressed loop from a keyReleased event.

http://docs.oracle.com/javase/tutori...ylistener.html
Apr 13 '13 #2

Post your reply

Sign in to post your reply or Sign up for a free account.