变量未正确更新
Variables not updating properly
这是一个逻辑问题,所以不幸的是,我还没有真正找到任何关于我做错的地方。几乎一天以来,我一直在试图弄清楚为什么 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。
问题是您的 CoreProcesses
run
方法没有被执行,因为您没有在 Thread
创建中指定 Runnable
对象。
所以,而不是
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();
}
这是一个逻辑问题,所以不幸的是,我还没有真正找到任何关于我做错的地方。几乎一天以来,我一直在试图弄清楚为什么 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。
问题是您的 CoreProcesses
run
方法没有被执行,因为您没有在 Thread
创建中指定 Runnable
对象。
所以,而不是
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();
}