Jump to content
  • Advertisement
Sign in to follow this  
Zaku

a submarine java game (JFrame)

This topic is 4088 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!