我正在AndEngine中开发我几乎已经完成了我的游戏,但不幸的是,由于我在更新循环中检查了很多事情,因此偶尔也会遇到低fps。我现在明白这是问题所在,而且我一直试图用Box2d改变它,如理论上,这正是我需要的,但我无法掌握它!
基本上,我有4个阵列;一个用于汽车,三个用于敌人。汽车从左向右行驶,如果他们与这些敌人中的任何一个接触,它的速度就会变化。当我加载阵列时,是否必须为每个汽车和敌人分配一个物体?然后我该如何检查?我运行for循环,然后运行'isBodyContacted(carBody,iceBergBody);'在更新看?这有点过分了!
供参考,加载我的车:
private void loadCar() {
for (int i = 0; i < rManager.getInstance().carArray.length; i++) {
rManager.getInstance().carArray[i] = new Car(new Sprite(
rManager.getInstance().spawnPoint[i].getSpawnPos().x,
rManager.getInstance().spawnPoint[i].getSpawnPos().y,
rManager.getInstance().car_region,
engine.getVertexBufferObjectManager()) {
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent,
final float pTouchAreaLocalX,
final float pTouchAreaLocalY) {
if (pSceneTouchEvent.isActionMove()
&& GameManager.getInstance().getDebugMode() == true) {
this.setPosition(pSceneTouchEvent.getX(),
pSceneTouchEvent.getY());
}
return true;
}
});
}
for (int i = 0; i < rManager.getInstance().carArray.length; i++) {
rManager.getInstance().carArray[i].getCarSprite().setScale(0.5f);
rManager.getInstance().carArray[i].getCarSprite().setAnchorCenter(
0, 0);
colourX = (randNumber(2, 9));
colourY = (randNumber(2, 9));
colourZ = (randNumber(2, 9));
colourX /= 10;
colourY /= 10;
colourZ /= 10;
rManager.getInstance().carArray[i].getCarSprite().setColor(colourZ,
colourY, colourZ);
attachChild(rManager.getInstance().carArray[i].getCarSprite());
this.registerTouchArea(rManager.getInstance().carArray[i]
.getCarSprite());
this.setTouchAreaBindingOnActionDownEnabled(true);
}
}
装载敌人..
void loadIceBerg() {
for (int i = 0; i < rManager.getInstance().iceBergArray.length; i++) {
rManager.getInstance().iceBergArray[i] = new IceBergEnemy(
new Sprite(randNumber(200, 700), randNumber(0, 480),
rManager.getInstance().iceBerg_region,
engine.getVertexBufferObjectManager()) {
@Override
public boolean onAreaTouched(final TouchEvent pSceneTouchEvent, final float pTouchAreaLocalX, final float pTouchAreaLocalY) {
this.setPosition(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
if (this.getY() > rManager.getInstance().camera.getHeight() - (this.getHeight() / 2) || this.getY() < 0 + (this.getHeight() / 2))
{
this.setPosition(200 - (pDifficultyLevel * 20),
-100);
showScore(50, " ice berg!");
this.setVisible(false);
}
if(pSceneTouchEvent.isActionUp())
{
updateScore(50);
GameManager.getInstance().updateTotalIceBergEnemiesKilled();
}
return true;
}
});
rManager.getInstance().iceBergArray[i].getIceBergSprite()
.setPosition(
rManager.getInstance().iceBergArray[i]
.getIceBergSprite().getX(),
rManager.getInstance().iceBergArray[i]
.getIceBergSprite().getY());
rManager.getInstance().iceBergArray[i].getIceBergSprite().setScale(
0.8f);
this.registerTouchArea(rManager.getInstance().iceBergArray[i]
.getIceBergSprite());
this.setTouchAreaBindingOnActionDownEnabled(true);
attachChild(rManager.getInstance().iceBergArray[i]
.getIceBergSprite());
}
}
有些碰撞!
else if (rManager.getInstance().iceBergArray[2]
.getIceBergSprite().collidesWith(
rManager.getInstance().carArray[r]
.getCarSprite())) {
rManager.getInstance().carArray[r].setCarSpeed(0f);
}
else if (rManager.getInstance().iceBergArray[3]
.getIceBergSprite().collidesWith(
rManager.getInstance().carArray[r]
.getCarSprite())) {
rManager.getInstance().carArray[r].setCarSpeed(0f);
}
else if (rManager.getInstance().iceBergArray[4]
.getIceBergSprite().collidesWith(
rManager.getInstance().carArray[r]
.getCarSprite())) {
rManager.getInstance().carArray[r].setCarSpeed(0f);
}
else if (rManager.getInstance().iceBergArray[5]
.getIceBergSprite().collidesWith(
rManager.getInstance().carArray[r]
.getCarSprite())) {
rManager.getInstance().carArray[r].setCarSpeed(0f);
}
我找到了很多教程,但似乎都没有引用一个对象数组,这正是我需要的。如果有人可以帮助我或提供某种快速教程,那将非常感激。谢谢!
答案 0 :(得分:0)
您不应使用任何循环来检查每种可能的碰撞组合。相反,您应该为整个世界注册一个监听器并在您的身体上设置用户数据(例如type =“car”,“enemy”)。然后在监听器中,您只需检查哪两个对象发生碰撞。看看这个例子:
http://www.andengine.org/forums/gles2/collision-events-t7140.html#p31300