AndEngine,附加影响碰撞的同一实体中的2个

时间:2013-08-09 16:38:37

标签: object andengine collision

我正在创建一个类似于Glider PRO的游戏。在游戏中,您可以控制纸飞机,并使用通风口防止您摔倒在地板上。当飞机与通风口发生碰撞时(使用碰撞方法),它会将飞机向上推。现在问题是,当我将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;
}

1 个答案:

答案 0 :(得分:0)

我发现了问题。 checkCollison()方法有效,但当我添加第二个AirVent时,if / else语句相互争斗。从checkCollision()方法中删除else语句修复了问题但是现在,这不是我希望我的游戏行为的方式。