0

I am trying to make my first game in Java. I have my canvas and a little circle player that can move. When I press the Left arrow key, the character moves Up and to the Left. When I press Right, the character moves Down and to the Right. Up and Down arrows do nothing at all. I have checked to see of i got any of my variables confused but I do not think I did. I am very new to this so if anyone can show me why and where I went wrong, that would be much helpful

My Game.java is this:

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.image.BufferedImage;

public class GamePanel extends JPanel implements Runnable, KeyListener{

    // Fields
    public static int WIDTH = 600;
    public static int HEIGHT = 600;

    private Thread thread;
    private boolean running;

    private BufferedImage image;
    private Graphics2D g;

    private int FPS = 30;
    private double averageFPS;

    private Player player;

    // Constructor
    public GamePanel() {
        super();
        setPreferredSize(new Dimension(WIDTH, HEIGHT));
        setFocusable(true);
        requestFocus();
    }

    // Functions
    public void addNotify() {
        super.addNotify();
        if (thread == null) {
            thread = new Thread(this);
            thread.start();
        }
        addKeyListener(this);
    }

    public void run() {
        running = true;

        image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
        g = (Graphics2D) image.getGraphics();

        player = new Player();

        long startTime;
        long URDTimeMillis;
        long waitTime;
        long totalTime = 0;

        int frameCount = 0;
        int maxFrameCount = 30;

        long targetTime = 1000 / FPS;

        // Game Frame Limiter and Timer
        while (running) {

            startTime = System.nanoTime();

            gameUpdate();
            gameRender();
            gameDraw();

            URDTimeMillis = (System.nanoTime() - startTime) / 1000000;

            waitTime = targetTime - URDTimeMillis;

            try {
                Thread.sleep(waitTime);
            } catch (Exception ignored){

         }

         totalTime += System.nanoTime() - startTime;
         frameCount ++;
         if (frameCount == maxFrameCount) {
                averageFPS = 1000.0 / ((totalTime / frameCount) / 1000000);
                frameCount = 0;
                totalTime = 0;
            }
        }
    }

    private void gameUpdate() {

        player.update();

    }

    private void gameRender() {

        g.setColor(Color.WHITE);
        g.fillRect(0, 0, WIDTH, HEIGHT);
        g.setColor(Color.BLACK);
        g.drawString("FPS: " + averageFPS, 10, 10);

        player.draw(g);

    }

    private void gameDraw() {
        Graphics g2 = this.getGraphics();
        g2.drawImage(image, 0, 0, null);
        g2.dispose();
    }

    public void keyTyped(KeyEvent key){

    }

    public void keyPressed(KeyEvent key){

        int keyCode = key.getKeyCode();
        if (keyCode == KeyEvent.VK_LEFT){
            player.setLeft(true);
        }
        if (keyCode == KeyEvent.VK_RIGHT){
            player.setRight(true);
        }
        if (keyCode == KeyEvent.VK_UP){
            player.setUp(true);
        }
        if (keyCode == KeyEvent.VK_DOWN){
            player.setDown(true);
        }

    }

    public void keyReleased(KeyEvent key){

        int keyCode = key.getKeyCode();
        if (keyCode == KeyEvent.VK_LEFT){
            player.setLeft(false);
        }
        if (keyCode == KeyEvent.VK_RIGHT){
            player.setRight(false);
        }
        if (keyCode == KeyEvent.VK_UP){
            player.setUp(false);
        }
        if (keyCode == KeyEvent.VK_DOWN){
            player.setDown(false);
        }

    }

}

And this is my Player.java

import java.awt.*;

public class Player {

    // Fields
    private int x;
    private int y;
    private int r;

    private int dx;
    private int dy;
    private int speed;

    private boolean left;
    private boolean right;
    private boolean up;
    private boolean down;

    private int lives;
    private Color color1;
    private Color color2;

    // Construtor
    public Player(){

        x = GamePanel.WIDTH /2;
        y = GamePanel.HEIGHT /2;
        r = 5;

        dx = 0;
        dy = 0;
        speed = 5;

        lives = 3;
        color1 = Color.WHITE;
        color2 = Color.BLACK;

    }

    // Functions
    public void setLeft(boolean b){left = b;}
    public void setRight(boolean b){right = b;}
    public void setUp(boolean b){up = b;}
    public void setDown(boolean b){down = b;}

    public void update(){

        if (left){
            dx = -speed;
        }
        if (right){
            dx = speed;
        }
        if (up){
            dy = speed;
        }
        if (down){
            dy = -speed;
        }

        x += dx;
        y += dy;

        if (x < r){
            x = r;
        }
        if (y < r){
            y = r;
        }
        if (x > GamePanel.WIDTH - r){
            x = GamePanel.WIDTH - r;
        }
        if (y > GamePanel.HEIGHT - r){
            y = GamePanel.HEIGHT - r;
        }

        dx = 0;
        dy = 0;

   }

    public void draw(Graphics2D g){

        g.setColor(color1);
        g.fillOval(x - r, x - r, 2 * r, 2 * r);

        g.setStroke(new BasicStroke(3));
        g.setColor(color1.darker());
        g.drawOval(x - r, x - r, 2 * r, 2 * r);
        g.setStroke(new BasicStroke(1));

    }

}
Frakcool
  • 10,915
  • 9
  • 50
  • 89
Stone Monarch
  • 325
  • 2
  • 15
  • Maybe try to reset your player's `left`, `right` etc members back to `false` at the end of `Player.update()` instead of relying on the `keyReleased()` callback. Just to make sure the issue isn't simply the key releases not registering correctly. – domsson Mar 08 '17 at 14:44
  • Possible duplicate of [How to use Key Bindings instead of Key Listeners](http://stackoverflow.com/questions/22741215/how-to-use-key-bindings-instead-of-key-listeners) – user1803551 Mar 08 '17 at 14:58
  • @domdom i tried that and it just makes the player less responsive (Slower to get moving) but it still moves Up and Left, and Down and Right. Up and Down arrows do nothing – Stone Monarch Mar 09 '17 at 04:37

1 Answers1

0

In the Draw method of the player the line should be:

g.fillOval(x - r, y - r, 2 * r, 2 * r);

// and

g.drawOval(x - r, y - r, 2 * r, 2 * r);

not

g.fillOval(x - r, x - r, 2 * r, 2 * r);

// and

g.drawOval(x - r, x - r, 2 * r, 2 * r);

The X and Y vars were mixed up and for some reason caused the character to move differently. Works now

Stone Monarch
  • 325
  • 2
  • 15