2

振り子のGameActivityを(AndEngineで)書いています。

適用中にCollisionHandler、これは1つではなく一連の衝突イベントを生成しますか?実は、スプライトがぶつかったときに一度だけ実行してほしいです。

私の場合:とmovingBall衝突するとmLeftWall、それ5 timesonCollision();と呼ばれます。それは異常でした、私は一度だけ期待しました。

ContactListener代わりに使用して登録する必要がありPhysicsWorldますか?これが私のコードです:

MyGameActivity.java:

/** List of objects to check collision of any animating objects. E.g: walls  */
private ArrayList<IShape> mCollidingTargetList = new ArrayList<IShape>();

/** user-defined collision handler */
PendulumCollisionCallback collideActionCallback = new PendulumCollisionCallback();

@Override
public Scene onCreateScene() 
{
    final Scene scene = super.onCreateScene();

    final VertexBufferObjectManager vertexBufferObjectManager = this.getVertexBufferObjectManager();

    //Create walls
    mWallLeft = new Rectangle(0, 0, 2, mCameraHeight, vertexBufferObjectManager);
    mWallRight = new Rectangle(mCameraWidth - 2, 0, 2, mCameraHeight, vertexBufferObjectManager);
    PhysicsFactory.createBoxBody(mPhyscisWorld, mWallLeft, BodyType.StaticBody, mWallFixtureDef);
    PhysicsFactory.createBoxBody(mPhyscisWorld, mWallRight, BodyType.StaticBody, mWallFixtureDef);
    scene.attachChild(mWallLeft);
    scene.attachChild(mWallRight);

    //Create movingBall - the pendulum ball
    final AnimatedSprite movingBall = new AnimatedSprite(mCenterX, mCenterY, this.mCircleFaceTextureRegion, this.getVertexBufferObjectManager());
    final Body ballBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld, movingBall, BodyType.DynamicBody, mObjectFixtureDef);
    movingBall.setUserData(ballBody); // for getting the body attached with UI

    //..... Stuff to create anchorBody and RevoluteJoint with the movingBall

    //list of bodies where sprites collide
    this.mCollidingTargetList.add(mWallLeft);
    this.mCollidingTargetList.add(mWallRight);

    //Manage user-defined collision handler: movingBall collides with walls
    CollisionHandler pendulumCollideHandler = new CollisionHandler(collideActionCallback, movingBall, mCollidingTargetList);
    scene.registerUpdateHandler(pendulumCollideHandler);

    return scene;
}

PendulumCollisionCallback.java:

public class PendulumCollisionCallback implements ICollisionCallback
{
    public PendulumCollisionCallback()
    { }

    /**
     * @param animatedShape
     *      Entity to check collision with other targets
     * @param pTargetShape
     *      Target to check the collision
     * @return <code>true</code> to proceed, <code>false</code> to stop further collosion-checks.
     */
    @Override
    public boolean onCollision(IShape animatedShape, IShape pTargetShape) 
    {
        String pendulPos = String.format("Pendul:x=%f, y=%f", animatedShape.getX(), animatedShape.getY());      
        Log.d("COLLIDE", pendulPos);
        return false;
    }
}
4

1 に答える 1

2

問題が見つかりました。これはセパレートPhysics HandlerとSceneのUpdateHandler作業シーケンスによるものです。

がボディの衝突を制御している間PhysicsWorld、ボディはまだ数フレーム前に移動している => UI スプライト (このボディにアタッチされている) の位置が衝突オブジェクトとの接合部にあるようにします。これらのフレームの間、updateHandlerは によって衝突イベントを生成しCallbackHandlerます。

解決策は、最初の衝突時に設定され、衝突が終了したときに設定解除されるブール フラグを追加するトリッキーな方法です(ただし、これは適切ではありません)。

P/S: 私のフレームレート観測フォームFPSLogger~88,9 FPS.

于 2012-09-05T03:53:17.787 に答える