java - 为什么AndEngine的CollisionHandler在一次碰撞中会产生多个事件?

标签 java android collision-detection andengine

我正在编写一个 Swing 钟摆的GameActivity(在AndEngine中)。

在应用CollisionHandler时,这将产生一系列碰撞事件,而不是一个?实际上,我希望它在 Sprite 碰撞时仅执行一次

我的案例:当movingBallmLeftWall碰撞时,调用 onCollision()5 次;很不正常,我只预料到了一次。

我应该使用 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;
    }
}

最佳答案

我发现了这个问题。这是因为 Physics Handler 和 Scene 的 UpdateHandler 工作顺序是分开的。

PhysicsWorld仍在控制物体碰撞时,物体仍在向前移动几帧=>,以便UI Sprite (与该物体相连)的位置与碰撞对象相连接。在这些帧期间,updateHandler 通过 CallbackHandler 生成碰撞事件。

解决方案是添加 boolean 标志,该标志在第一次碰撞时设置并在完成碰撞时取消设置(尽管这不是不错)。

P/S:我从FPSLogger观察到的帧速率是~88,9 FPS

关于java - 为什么AndEngine的CollisionHandler在一次碰撞中会产生多个事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12087523/

相关文章:

android - 如何获取我的应用程序的移动数据使用情况(例如使用 TrafficStats)?

javascript - 碰撞检测不应该使物体传送

javascript - 如何让 JavaScript 中的碰撞函数正常工作?

c# - 一维数组碰撞

java - Android 上的 JainSip : Sending REGISTER throws Exception

java - 如何在 Selenium WebElement 中搜索?

java - Object StorageAsyc 不等待完成

java - Android中的圆形图像水平 ScrollView ,以便当我到达最后一个图像时,第一个图像会出现,当我到达第一个图像时,最后一个图像会出现

android - 最新版本如何使用getdownloadurl?

java - 按下后退按钮时 Android View.getContext() 出现空对象错误