Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: help me!

  1. #1
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Angry help me!

    Arrrgh! This is hopeless! My dream is to be a game developer, and I have tried to make a simple 2D game. Every single time, I tell you, every single time, I get stuck with something. Before my imageIO hasn't been working, so I decided to make a game with only javas Graphics2D. Just when I got started I encounter a problem, my game loop doesn't work. I use a simple Thread game loop. I put print messages in my code to debug that way. The loop runs once and then stop! Why?

    Here is my code:

    package shooter;
     
    import java.awt.Color;
    import java.awt.Graphics;
    import java.awt.Graphics2D;
    import java.awt.event.KeyAdapter;
    import java.awt.event.KeyEvent;
     
    import javax.swing.JPanel;
     
    public class GamePanel extends JPanel implements Runnable {
     
    	private boolean paused = false;
    	private boolean gameover = false;
    	private int playerX, playerY, playerW, playerH, playerDx;
    	private Thread thread;
     
    	public GamePanel() {
    		thread = new Thread(this);
     
    		playerX = Game.WIDTH / 2 - 15;
    		playerY = 360;
    		playerW = 30;
    		playerH = 20;
     
    		addKeyListener(new KeyL());
     
    		thread.start();
    	}
     
    	public void paint(Graphics gf) {
     
    		Graphics2D g = (Graphics2D)gf;
     
    		//paints the background.
    		g.setColor(Color.BLACK);
    		g.fillRect(0, 0, Game.WIDTH, Game.HEIGHT);
     
    		//paints the player
    		g.setColor(Color.WHITE);
    		g.fillRect(playerX, playerY, playerW, playerH);
     
    		System.out.println("painted!");
    	}
     
    	//game loop
    	@Override
    	public void run() {
    		move();
    		repaint();
     
    		System.out.println("looped!");
    		try {
    			Thread.sleep(17);
    		}catch(Exception ex){
    			System.out.println(ex);
    		}
    	}
     
    	//checks all collisions in game
    	public void checkCollisions() {
     
    		//checks if the player its the walls.
    		if (playerX + playerDx < 0) {
    			playerX = 0;
    			playerDx = 0;
    		}
    		else if (playerX + playerDx > Game.WIDTH) {
    			playerX = Game.WIDTH - playerW;
    			playerDx = 0;
    		}
    	}
     
    	//moves all entities.
    	public void move() {
    		playerX += playerDx;
    		System.out.println("Moved!");
    	}
     
    	private class KeyL extends KeyAdapter {
     
    		public void keyPressed(KeyEvent e) {
    			int key = e.getKeyCode();
     
    			if (key == KeyEvent.VK_A) {
    				playerDx = -2;
    				System.out.println("moving left");
    			}
    			if (key == KeyEvent.VK_D) {
    				playerDx = 2;
    				System.out.println("moving right");
    			}
    		}
    	}
    }

    Yes, I know there is no main class. That one is in the JFrame class but I know the error isn't there.

    This is very frustrating! Earlier I have just given up, but know I decided to get help instead.

    Please, please, PLEASE, help me!

    P.S. The controls doesn't work either.
    Last edited by Dr.Code; June 26th, 2012 at 12:27 PM.

  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: help me!

    A Thread's run() method is only called once, not repeatedly. If you want it to run more than once, you have to set up a loop inside of it.

    I would recommend that you use a Swing Timer instead though.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. The Following User Says Thank You to KevinWorkman For This Useful Post:

    Dr.Code (June 26th, 2012)

  4. #3
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default Re: help me!

    Quote Originally Posted by KevinWorkman View Post
    A Thread's run() method is only called once, not repeatedly. If you want it to run more than once, you have to set up a loop inside of it.

    I would recommend that you use a Swing Timer instead though.
    Thank you! It worked! Sort of... The controls don't work? I don't know why. Anyone that can help me?

  5. #4
    Member
    Join Date
    Feb 2011
    Posts
    55
    My Mood
    Tolerant
    Thanks
    1
    Thanked 16 Times in 15 Posts

    Default Re: help me!

    You need to set the focus to the JPanel,
    Make it focusable first and then request the focus.

    You may also want to make it so the player stops moving when the keys are released. Unless your intent was to have them always moving.
    Last edited by JJeng; June 26th, 2012 at 02:45 PM.

  6. The Following User Says Thank You to JJeng For This Useful Post:

    Dr.Code (June 27th, 2012)

  7. #5
    Member
    Join Date
    Jun 2012
    Location
    Uppsala, Sweden
    Posts
    36
    Thanks
    10
    Thanked 1 Time in 1 Post

    Default Re: help me!

    Quote Originally Posted by JJeng View Post
    You need to set the focus to the JPanel,
    Make it focusable first and then request the focus.
    Thank you!

    Quote Originally Posted by JJeng View Post
    You may also want to make it so the player stops moving when the keys are released. Unless your intent was to have them always moving.
    Yeah, I forgot that .

Tags for this Thread