Sign in to follow this  
Zaku

a submarine java game (JFrame)

Recommended Posts

ok, im making a submarine game where im shooting a depth charge down to the submarines, killing them. i need to know how to make the depth charge go down continuously after hitting the space button, until hitting the target. what i got is:
public void keyPressed(KeyEvent e)
	{
		switch(e.getKeyCode())
		{
			
			case KeyEvent.VK_LEFT:x=x-speed;break;//if you hit left, it goes left
			
			case KeyEvent.VK_RIGHT:x=x+speed;break;//same with right
			
			case KeyEvent.VK_SPACE:shoty=shoty+15;if(shoty>300){shoty=50;};break;
		}
		
		
		
	}
itll only go down when i hit the button with this code. shoty is the y coordinate for the depth charge. what should i do to make it go continuously down, after hitting space? thank you. made in jframe btw

Share this post


Link to post
Share on other sites
You need to move the position-altering code outside of the key event. Put it into some kind of update function (preferably inside a class). Add a new boolean variable (bMove or whatever), set to false. When the key is pressed, set bMove to true. In your update function, you would have something like

if (bMove) {
/* Your position-update code goes here */
}


Since you check if it's at a maximum depth, move that into your position-update code as well and set bMove to false if it's past a certain depth.

Good luck!

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this