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