Movement Issues

I have been following some tutorials, trying to get into programming in Java and so far all has worked well. The problem I have now stumbled upon is, I am not able to move my image around in my window with the arrow keys after proceeding with the tutorials into threads. I have followed the code, in a manner I thought was precise, and yet, my image will not move with the arrow keys being pressed down. Can anyone point out what I’m doing wrong?

package javagame;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

import javax.swing.ImageIcon;
import javax.swing.JFrame;

public class JavaGame extends JFrame implements Runnable{

	int x, y, xDirection, yDirection;
	private Image dbImage;
	private Graphics dbg;
	Image face;
	
	public void run(){
		try{
			while(true){
				move();
				Thread.sleep(5);
			}
		}
		catch(Exception e){
			System.out.println("error...");
		}
	}
	
	// handle movement thread.
	public void move() {
		x += xDirection;
		y += yDirection;
		
		if (x <= 0);
		     x = 0;
		if (x >= 50);
		     x = 50;
		if (y <= 0);
		     y = 0;
		if (y >= 50);
		     y = 50;
	}

	// set up the directional movement variables of the player.
	public void setXDirection(int xdir) {
		xDirection = xdir;
	}
	public void setYDirection(int ydir) {
		yDirection = ydir;
	}

	// handle key input.
	public class AL extends KeyAdapter {
		public void keyPressed(KeyEvent e) {
			int keyCode = e.getKeyCode();
			// handle arrow key movement.
			if (keyCode == e.VK_LEFT) {
				setXDirection(-1);
			}
			if (keyCode == e.VK_RIGHT) {
				setXDirection(+1);
			}
			if (keyCode == e.VK_UP) {
				setYDirection(-1);
			}
			if (keyCode == e.VK_DOWN) {
				setYDirection(+1);
			}

		}

		public void keyReleased(KeyEvent e) {
			int keyCode = e.getKeyCode();
			if (keyCode == e.VK_LEFT) {
				setXDirection(0);
			}
			if (keyCode == e.VK_RIGHT) {
				setXDirection(0);
			}
			if (keyCode == e.VK_UP) {
				setYDirection(0);
			}
			if (keyCode == e.VK_DOWN) {
				setYDirection(0);
			}
		}
	}

	// handle the window class.
	public JavaGame() {
		// load images.
		ImageIcon i = new ImageIcon(
				"C:/Users/Me/workspace/JavaGame/src/javagame/face.png");
		face = i.getImage();
		
		// game properties.
		addKeyListener(new AL());
		setLocation(400, 300);
		setTitle("JavaGame");
		setSize(256, 256);
		setResizable(false);
		setVisible(true);
		setBackground(Color.RED);
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		x = 50;
		y = 50;
	}

	// handle graphics input.
	public void paint(Graphics g) {
		dbImage = createImage(getWidth(), getHeight());
		dbg = dbImage.getGraphics();
		paintComponent(dbg);
		g.drawImage(dbImage, 0, 0, this);
	}

	public void paintComponent(Graphics g) {
		// draw and refresh the image.
		g.setColor(Color.RED);
		g.drawImage(face, x, y, this);
		repaint();
	}

	public static void main(String[] args) {
		JavaGame jg = new JavaGame();
		// Threads
		Thread t1 = new Thread(jg);
		t1.start();
	}

}

I was able to move the object earlier using the following code format.


if (x >= 200);
    x = 200;
else
    x += 5;

This, however was not best for the style I wanted to do. Where did I go wrong?

You put semi-colons after if statements! You shouldn’t do that, as that means that the if statement body is empty ;D

This way, your move method only causes X and Y to be permanently at 50,50 :slight_smile:

I’m trying to figure out how that would even compile, since the ‘else’ should be unexpected at that point.

Fantastic. That’s exactly what it was. After reviewing the tutorial he was doing I saw that he did in fact place the semi-colons there himself, only to remove them later on while his video was paused to correct other code. Thank you for your help. Now I just need to figure out how to make the controls a little less glitchy. :smiley:

no ‘else’ in his full code.

maybe we should publish more about pastebin feature.

There’s an “else” in that second code snippet :cranky:

Yeah I’m still fairly new to the Java world so I make simple mistakes sometimes. :smiley: Especially when I’m trying to follow someone who talks and types fairly quickly. The only thing I’m not liking about this so far is the fact that it’s fairly glitchy in the movement. Basically when you move in the four general directions of up, down, left, and right, it moves smooth and crisply. When you move in a diagonal direction, it catches for a moment before continuing it’s motion. Is there a simple fix to this that anyone can guide me towards or any suggestions to fix it?

Simply add brackets:


// handle movement thread.
	public void move() {
		x += xDirection;
		y += yDirection;
		if (x <= 0) {
			x = 0;
		}
		if (x >= 50) {
			x = 50;
		}
		if (y <= 0) {
			y = 0;
		}
		if (y >= 50) {
			y = 50;
		}
	}

that’s the thing. I have added brackets already and it had no effect in making the movement less glitchy.

That’s not his main code part. Guess he typed it while posting.

@OP: how about your framerate?

I’m not sure about my frame-rate. I have no code implemented to tell me what it is, nor do I know of any built in function that may exist.

Sometimes fps can be the root. Too much or less frame in a second can cause bad move. However it’s easily fixed by change your speed value.

You are saying if x is less than or equal to zero, set it to 0, which means it is going to get caught in a loop and you won’t be able to move in the x direction.


	public void move() {
		x += xDirection;
		y += yDirection;
		if (x <= 0) {   //change to if (x < 0)
			x = 0;
		}
		if (x >= 50) { //change to if (x > 50)
			x = 50;
		}
		if (y <= 0) { // change to if (y < 0)
			y = 0;
		}
		if (y >= 50) { // change to if (y > 50)
			y = 50;
		}
	}

That has it fixed. Thank you very much.

Actually it should never be stuck in a loop, because you are adding xDirection and yDirection before your if checks.

Ahh didn’t notice that.