当我遇到一个不寻常的问题时,我正在关注Java的一些基本游戏设计教程。当我运行一个简单地在屏幕上移动基于图像的角色的applet时,它不会让我使用这些方法来改变图像的位置。它在我的robot.update()方法中引发了一个NullPointerException,它处理图像的移动。奇怪的是,如果我从Applet框架的下拉列表中选择“重新启动”选项,它将正常工作。有什么想法吗?
主类
package Game;
import java.applet.Applet;
import java.awt.Color;
import java.awt.Frame;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.net.URL;
public class First extends Applet implements Runnable, KeyListener {
private Robot robot;
private Image image, character;
private Graphics second;
private URL base;
@Override
public void init() {
setSize(800,480);
setBackground(Color.BLACK);
setFocusable(true);
Frame frame = (Frame) this.getParent().getParent();
frame.setTitle("First Game");
addKeyListener(this);
try {
base = getDocumentBase();
} catch (Exception e) {
e.printStackTrace();
System.out.println("Image not accessible");
}
character = getImage(base, "data/character.png");
// TODO Auto-generated method stub
super.init();
}
@Override
public void start() {
Thread thread = new Thread(this);
thread.start();
robot = new Robot();
// TODO Auto-generated method stub
super.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
while (true) {
robot.update();
repaint();
try {
Thread.sleep(17);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
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) {
// TODO Auto-generated method stub
g.drawImage(character, robot.getCenterX() - 61,
robot.getCenterY() - 63, this);
super.paint(g);
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
}
@Override
public void keyPressed(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
System.out.println("Move UP");
break;
case KeyEvent.VK_DOWN:
System.out.println("Move DOWN");
break;
case KeyEvent.VK_LEFT:
robot.moveLeft();
break;
case KeyEvent.VK_RIGHT:
robot.moveRight();
break;
case KeyEvent.VK_SPACE:
robot.jump();
break;
}
// TODO Auto-generated method stub
}
@Override
public void keyReleased(KeyEvent e) {
switch (e.getKeyCode()) {
case KeyEvent.VK_UP:
System.out.println("Stop UP");
break;
case KeyEvent.VK_DOWN:
System.out.println("Stop DOWN");
break;
case KeyEvent.VK_LEFT:
robot.stop();
break;
case KeyEvent.VK_RIGHT:
robot.stop();
break;
case KeyEvent.VK_SPACE:
robot.stop();
break;
// TODO Auto-generated method stub
}
}
}
机器人类
package Game;
public class Robot {
private int centerX = 100;
private int centerY = 382;
private boolean jumped = false;
private int speedX = 0;
private int speedY = 1;
public void update() {
if (speedX < 0) {
centerX += speedX;
} else if (speedX == 0){
System.out.println("Do not scroll the background");
} else {
if (centerX <= 300) {
centerX += speedX;
} else {
System.out.println("Scroll background here");
}
}
if (centerY + speedY >= 382) {
centerY = 382;
}else{
centerY += speedY;
}
// Handles Jumping
if (jumped == true) {
speedY += 1;
if (centerY + speedY >= 382) {
centerY = 382;
speedY = 0;
jumped = false;
}
}
// Prevents going beyond X coordinate of 0
if (centerX + speedX <= 60) {
centerX = 61;
}
}
public int getCenterX() {
return centerX;
}
public void setCenterX(int centerX) {
this.centerX = centerX;
}
public int getCenterY() {
return centerY;
}
public void setCenterY(int centerY) {
this.centerY = centerY;
}
public boolean isJumped() {
return jumped;
}
public void setJumped(boolean jumped) {
this.jumped = jumped;
}
public int getSpeedX() {
return speedX;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public int getSpeedY() {
return speedY;
}
public void setSpeedY(int speedY) {
this.speedY = speedY;
}
public void moveRight() {
speedX = 6;
}
public void moveLeft() {
speedX = -6;
}
public void stop() {
speedX = 0;
}
public void jump() {
if (jumped == false) {
speedY = -15;
jumped = true;
}
}
}
这是我收到的错误消息
Exception in thread "Thread-3" java.lang.NullPointerException
at Game.First.run(First.java:65)
at java.lang.Thread.run(Thread.java:722)
答案 0 :(得分:1)
首先应该在运行线程后初始化机器人对象,尝试:
robot = new Robot();
Thread thread = new Thread(this);
thread.start();
当您开始执行线程时,它可能以null robot开始。
当你再次重新加载时,它会被初始化。