go inside if statment onces every time.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • game2d
    New Member
    • Apr 2013
    • 59

    go inside if statment onces every time.

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

    Code:
    boolean space
    boolean shooting = false;
    
    if(space && !shooting)
    		{	
    			//shoot bullets
    			shooting = true;
    		}
    		if(!space)
    		{
    			shooting = false;
    		}

    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.
    Code:
    boolean space
    boolean shooting = false;
    
    if(space)
    		{	
    			//shoot bullets
    		m.setSpace(false);
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    This Swing Java Tutorial describes developing graphical user interfaces (GUIs) for applications and applets using Swing components

    Comment

    Working...