【发布时间】:2016-02-15 17:36:26
【问题描述】:
我正在开发一个生成多个“笔记”(精灵)的游戏。
笔记是随机创建的。它们中的每一个都具有随机速度,并在不同的线程中创建。 Notes 类是 sprite 类的子类。它有 2 个属性和 1 个方法:
- vel - 一个 Velocity2 对象,在 音符对象的速度
- pos - 一个 Vector2 对象,保存音符对象的 x 和 y 坐标。
- changepos() - 一种根据物体速度改变位置的方法
(由于隐私原因,我无法发布该课程的代码)
我目前有一个静态类“NoteStack”,它可以容纳多达 64 个对 Notes 对象的引用。
public class NoteStack {
public Notes[] note_array;
public int stack_len;
public NoteStack(){
note_array = new Notes[64];
stack_len = 0;
}
public void push(Notes n){
if(stack_len<64){
note_array[stack_len] = n;
stack_len++;
Gdx.app.log("push", "pushed");
}
}
public void delete_note(int pos){
if(note_array[pos] != null){
note_array[pos] = null;
for(int i = pos; i<stack_len; i++){
note_array[pos] = note_array[pos+1];
}
note_array[stack_len] = null;
stack_len = stack_len - 1;
}
}
}
这是我的“更新”功能的代码
public void update(float d, SpriteBatch b){
core.draw(b);
for(int i = 0; i< noteStack.stack_len; i++){
Gdx.app.log("update", "Update function running" + i);
noteStack.note_array[i].changePos(d);
noteStack.note_array[i].draw(b);
// scr_w - screen width , scr_h - screen height
if(noteStack.note_array[i].pos.x > scr_w || noteStack.note_array[i].pos.x < 0 || noteStack.note_array[i].pos.y > scr_h || noteStack.note_array[i].pos.y < 0){
noteStack.delete_note(i);
}
}
}
问题(如您所见)是,每当 NoteStack 中的一个便笺对象被删除(即调用 delete_note 方法)时,数组中的其他便笺对象都会受到影响。
因此我的问题是:在 LibGDX 中引用多个精灵(注释)对象的最佳方法是什么?
【问题讨论】:
-
我看不到其他 Notes 对象受到了怎样的影响。引用它们的数组受到影响。我确实注意到,无论何时删除一个,都会移动数组的索引,以便跳过 for 循环中的下一个。此外,几乎肯定不需要多线程。