1
我正在使用acm库(acm.jar),同一个库斯坦福大学的学生用它们的Java课程CS106A来创建图形化应用程序更简单。使用Java线程创建测试碰撞检测的对象
acm.jar可以在这里找到:http://jtf.acm.org/
下面的代码将一个字符精灵图形窗口。如果我按z,角色会自动移动他的弓箭,并开始通过使用Java线程垂直移动。迄今为止没有错误。
现在我想能够在线程GImage object(linkArrow)
上执行碰撞检测。在我的程序发生了一个错误,当我试着这样做:
arrowPoint = new GPoint(linkArrow.getX(),linkArrow.getY());
arrowObject = getElementAt(arrowPoint);
我得到这个错误:
Exception in thread "AWT-EventQueue-1" java.lang.NullPointerException
您可以按Ctrl + F和类型的错误,看看我的程序的错误位置。
我已经做了碰撞检测使用使用GPoint
和getElementAt
我的游戏“突围”的图形矩形和椭圆形之前,从来没有使用GPoint
和getElementAt
问题。
这是我的代码:第一类是运行线程的主程序。第二类是线程。
import java.awt.event.KeyEvent;
import acm.graphics.GImage;
import acm.program.GraphicsProgram;
import acm.util.RandomGenerator;
public class Link extends GraphicsProgram {
private static final double GRAVITY = 1;
public void init(){
setSize(APPLICATION_WIDTH, APPLICATION_HEIGHT);
addLink();
addKeyListeners();
}
private void addLink(){
linkCharacter = new GImage("link sprites/linkFaceRight/link_frame_1_face_right.png");
add(linkCharacter,link_Location_XCoord,link_Location_YCoord);
}
public void keyPressed(KeyEvent e){
/* Link's Movement
*
*/
char linkMoveRightKey = e.getKeyChar();
if(linkMoveRightKey == 'z')
{
xSpeed = 0;
ySpeed = 0;
pause(arrowDELAY);
linkCharacter.move(xSpeed, ySpeed);
linkCharacter.setImage(linkAttackWithBow[linkFrame]);
linkFrame++;
callArrow();
// BUG
arrowPoint = new GPoint(linkArrow.getX(),linkArrow.getY());
arrowObject = getElementAt(arrowPoint);
}
//
/*
* summon link's arrow
*/
}
if(linkFrame>=linkAttackWithBow.length){
linkFrame = 0;
}
}
private void callArrow(){
if(linkFrame == 2){
linkArrow = new ArrowThread(SIZE, rgen.nextColor());
add(linkArrow,linkCharacter.getX(),linkCharacter.getY());
Thread arrowThread = new Thread(linkArrow);
arrowThread.start();
}
}
private ArrowThread linkArrow;
private int SIZE = 400;
private RandomGenerator rgen = RandomGenerator.getInstance();
private GImage gameBackgroundImage;
private GImage linkCharacter;
private double arrowDELAY = 28;
private int link_Location_XCoord = 50;
private int link_Location_YCoord = 50 ;
private final int APPLICATION_WIDTH = 1200;
private final int APPLICATION_HEIGHT = 800;
private String[] linkAttackWithBow = {"link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_1.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_2.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_3.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_1.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_2.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_3.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_1.png","link sprites/linkAttackWithBow/Link_Bow_Attack_Frame_2.png"};
private int linkFrame = 0;
private int xSpeed =5; //the number of pixels to move in x
private int ySpeed = 0; //0 so you only move horizontally
}
这里是我的Thread类产卵的箭头
import java.awt.Color;
import acm.graphics.GImage;
public class ArrowThread extends GImage implements Runnable{
public ArrowThread(int SIZE,Color color){
super("link sprites/linkAttackWithBow/arrow.png", SIZE,SIZE);
}
public void run(){
for(int i = 0; i < 1000/STEP;i++){
pause(60);
move(arrowSpeedX,arrowSpeedY);
}
}
private static final int arrowSpeedX = 0;
private static final int arrowSpeedY = -5;
private static final double STEP = 5;
}
我不明白。该代码只是在字符精灵执行linkAttackWithBow [2]时生成一个箭头线程。 – Nicholas
如果您在将'linkFrame'从其起始值0增加两次之前调用'linkArrow.getX()',则会引发错误。 – Bringer128