我正在创建一个类似于 Glider PRO 的游戏。在游戏中,您控制一架纸飞机,并使用通风口防止您跌倒在地板上。当飞机与通风口碰撞时,(使用 collidesWith 方法)它会将飞机向上推。现在问题来了,当我在场景中附加 2 个通风口时,只有第一个附加的通风口有效。第二个似乎没有与飞机相撞。我在每个通风口中使用 onManagedUpdate 方法来检查碰撞。这是我第一次使用 AndEngine 进行编码,因此将不胜感激任何帮助。
这是 AirVent 代码:
public class AirVent extends Rectangle
{
private Sprite sprite;
private Glider glider;
public AirVent(float pX, float pY, float pWidth, float pHeight,
VertexBufferObjectManager pVertexBufferObjectManager,
ITextureRegion pITextureRegion)
{
super(pX, pY, pWidth, pHeight, pVertexBufferObjectManager);
sprite = new Sprite(0, 0, pITextureRegion, pVertexBufferObjectManager);
sprite.setSize(pWidth, pWidth / 2);
sprite.setPosition((pWidth / 2) - (sprite.getWidth() / 2), pHeight
- sprite.getHeight());
this.attachChild(sprite);
this.setAlpha(0);
}
public Sprite getSprite()
{
return sprite;
}
public void checkCollision()
{
if (glider.collidesWith(this))
{
glider.setGravity(glider.getConstantGravity() * -1f);
}
else
{
glider.setGravity(glider.getConstantGravity());
}
}
public void applyGliderInteraction(Glider glider)
{
this.glider = glider;
}
@Override
public void onManagedUpdate(float secondsElapsed)
{
this.checkCollision();
super.onManagedUpdate(secondsElapsed);
}
}
和 MainActivity 代码部分:
@Override
protected Scene onCreateScene()
{
// TODO Auto-generated method stub
// 1 - Create new scene
final Scene scene = new Scene();
Sprite backgroundSprite = new Sprite(0, 0, this.mBackground, getVertexBufferObjectManager());
scene.attachChild(backgroundSprite);
roomChanger = new RoomChanger(0, 0, screenWidth, screenHeight, getVertexBufferObjectManager(), scene);
// 2 - Create the player
player1 = new Glider(339, 174, this.mPlayerGlider1, getVertexBufferObjectManager())
{
@Override
public void onManagedUpdate(float secondsElapsed)
{
if (player1.getLife() == 0)
{
// Execute your actions.
finish();
}
super.onManagedUpdate(secondsElapsed);
}
};
scene.attachChild(player1);
airVent = new AirVent(200, 100, 100, 400, getVertexBufferObjectManager(), this.mAirVent);
airVent.applyGliderInteraction(player1);
airVent2 = new AirVent(500, 100, 100, 400, getVertexBufferObjectManager(), this.mAirVent);
airVent2.applyGliderInteraction(player1);
scene.attachChild(airVent);
scene.attachChild(airVent2);
createControllers();
return scene;
}