变量未正确更新

时间:2015-02-01 21:02:09

标签: java logic

这是一个逻辑问题,所以我真的无法找到任何关于我做错的事情。现在差不多一天,我一直试图找出为什么centerX和centerY变量没有正确更新CoreProcesses文件中的播放器对象。每当我点击相应的按钮时,它们应该增加或减少适当的量,但由于某种原因,无论我尝试过什么,它都保持不变。我在这里完全失败了。我只需指向正确的方向,我提前感谢所有人。以下是文件

Player.java

package gameloopconcepts;

public class Player {

    /**
     *  Constants
     */
    final int MOVESPEED = 20;

    /**
     *  Class wide variables
     */

    // Player Draw Coordinates
    private int centerX = 90;
    private int centerY = 390;

    // Handles updating draw coordinates
    private int speedX = 0;
    private int speedY = 0;

    /**
     * Update method for game loop
     */

    public void update(){
        // Updates draw coordinates
        centerX += speedX;

    }

    /**
     * Movement Methods
     */

    public void moveUp(){
        speedY = -MOVESPEED;
    }

    public void moveDown(){
        speedY = MOVESPEED;
    }

    public void moveRight(){
        speedX = 20;
    }

    public void moveLeft(){
        speedX = -20;
    }

    public void stop(){
        speedX = 0;
    }

    /**
     * Getters and setters for variabes
     */

    public int getCenterX(){
        return centerX;
    }

    public int getCenterY(){
        return centerY;
    }

    public void setCenterX(int centerX){
        this.centerX = centerX;
    }

    public void setCenterY(int centerY){
        this.centerY = centerY;
    }

}

CoreProcesses.java

package gameloopconcepts;

import java.applet.Applet;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;

public class CoreProcesses extends Applet implements Runnable, KeyListener {

    // Constants

    // Class wide variables
    private Player player;
    private Image image, character;
    private Graphics second;
    private URL docBase;

    /**
     * Initializes the applet
     */
    @Override
    public void init() {
        // TODO Auto-generated method stub
        super.init();
        // Sets window size
        setSize(480, 800);

        // Sets window background color
        setBackground(Color.WHITE);

        // Sets the window to focus at the start
        setFocusable(true);

        // Sets the key listener
        addKeyListener(this);

        // Sets the applet frame and the title
        Frame frame = (Frame) this.getParent().getParent();
        frame.setTitle("ORIGIN ALPHA");

        // Gets document base
        try{
            docBase = getDocumentBase();
        } catch (Exception e){

        }

        // Sets up images
        character = getImage(docBase, "/Users/StudioAccount/Documents/Development/Workspaces/IntelliJ Idea/Test/src/assets/player.png");

    }

    /**
     * Starts the applet and assigns any necessary game objects
     */
    @Override
    public void start() {
        // TODO Auto-generated method stub
        super.start();
        player = new Player();

        // Defines the main thread and starts it
        Thread mainThread = new Thread();
        mainThread.start();
    }

    @Override
    public void stop() {
        // TODO Auto-generated method stub
        super.stop();
    }

    @Override
    public void destroy() {
        // TODO Auto-generated method stub
        super.destroy();
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub

        // Main Loop
        while(true){
            player.update();
            repaint();
            try{
                Thread.sleep(17);
            } catch (InterruptedException e){
                e.printStackTrace();
            }
        }

    }

    @Override
    public void update(Graphics g){
        if(image == null){
            image = createImage(this.getWidth(), this.getHeight());
            second = image.getGraphics();
        }

        second.setColor(getBackground());
        second.fillRect(0, 0, getWidth(), getHeight());
        second.setColor(getForeground());
        paint(second);

        g.drawImage(image, 0, 0, this);
    }

    @Override
    public void paint(Graphics g){
        g.drawImage(character, player.getCenterX(), player.getCenterY(), this);
    }

    /**
     * Key Listener events
     */
    @Override
    public void keyTyped(KeyEvent e) {
        // TODO Auto-generated method stub

    }

    @Override
    public void keyPressed(KeyEvent e) {
        // TODO Auto-generated method stub

        // Test logs for key events
        switch (e.getKeyCode()) {
            case KeyEvent.VK_W:
                player.moveUp();
                System.out.println(player.getCenterY());
                break;
            case KeyEvent.VK_S:
                player.moveDown();
                System.out.println(player.getCenterY());
                break;
            case KeyEvent.VK_A:
                player.moveLeft();
                System.out.println(player.getCenterX());
                break;
            case KeyEvent.VK_D:
                player.moveRight();
                System.out.println(player.getCenterX());
                break;
        }

    }

    @Override
    public void keyReleased(KeyEvent e) {
        // TODO Auto-generated method stub

        // Test logs for key events
        switch (e.getKeyCode()) {
            case KeyEvent.VK_W:
                System.out.println("Stop moving character up");
                break;
            case KeyEvent.VK_S:
                System.out.println("Stop moving character down");
                break;
            case KeyEvent.VK_A:
                player.stop();
                System.out.println("Stop moving character left");
                break;
            case KeyEvent.VK_D:
                player.stop();
                System.out.println("Stop moving character right");
                break;
        }

    }

}
按下相应的按钮,

centerX和centerY应该上升或下降20。

1 个答案:

答案 0 :(得分:1)

问题是您的CoreProcesses run方法没有被执行,因为您没有在Runnable创建中指定Thread对象

所以,而不是

Thread mainThread = new Thread();
你应该

Thread mainThread = new Thread(this);

另外,你拥有它的方式,如果按一次键,你会看到X位置的巨大增加,因为这种方法实际上被执行了很多次。

你可能想要的是update玩家keyPressed事件上的@Override public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { case KeyEvent.VK_W: player.moveUp(); System.out.println(player.getCenterY()); break; case KeyEvent.VK_S: player.moveDown(); System.out.println(player.getCenterY()); break; case KeyEvent.VK_A: player.moveLeft(); System.out.println(player.getCenterX()); break; case KeyEvent.VK_D: player.moveRight(); System.out.println(player.getCenterX()); break; } player.update(); }

{{1}}