在這個遊戲中,我試圖讓多個激光產生在給定的座標上,但由於某種原因,每次numLasers大於1時,應用都會崩潰。我嘗試過所有事情,而且我真的可以使用一些幫助。Android遊戲產卵激光精靈
這裏是我的代碼:
public class LaserSpawn {
private int amountOfVisibleLasers;
private Context context;
private long timeLastCreatedLaser;
private Laser[] holderLaser;
public LaserSpawn(Context context, int numLasers){
this.context = context;
holderLaser = new Laser[numLasers];
this.amountOfVisibleLasers = numLasers;
for(int i = 0; i< numLasers; i++){
holderLaser[0] = new Laser(context, -10, -10);
}
}
public void updatePlayerLaser(boolean shootLaser, float x, float y) {
// Check if a new Laser should be created
if(shootLaser == true) {
if(timeLastCreatedLaser + 100 < System.currentTimeMillis()) {
timeLastCreatedLaser = System.currentTimeMillis();
boolean createdNewLaser = false;
for(int i = 0; i < this.amountOfVisibleLasers; i++) {
if(createdNewLaser == false) {
if(holderLaser[i].isDisposed()) {
this.generateNewLaser(i,x,y);
createdNewLaser = true;
}
}
}
}
}
// Update all the other Lasers
for(int i = 0; i < this.amountOfVisibleLasers; i++) {
if(holderLaser[i].isDisposed() == false) {
holderLaser[i].update();
}
}
}
private void generateNewLaser(int i, float x, float y) {
holderLaser[i].setY(y);
holderLaser[i].setX(x);
}
謝謝!
你應該看看logcat的,看看它爲什麼崩潰。你能找到併發布堆棧跟蹤嗎? – Tim
如果我的答案有幫助並解決了問題,請點擊旁邊的勾號接受它 –