AndEngine Box2D。删除联合碰撞后的崩溃游戏

时间:2013-03-14 08:56:23

标签: android box2d andengine

请帮忙!删除Joint with collides后我的游戏崩溃。

游戏是挂在绳子上的身体。手指剪断了绳子,游戏崩溃了!

我的代码:

@Override
protected Scene onCreateScene() {

    this.mEngine.registerUpdateHandler(new FPSLogger());

    this.mScene = new Scene();
    this.mScene.setBackground(new Background(0, 0, 0));     

    this.mPhysicsWorld = new PhysicsWorld(new Vector2(0, SensorManager.GRAVITY_EARTH), false);

    /* Create the face and add it to the scene. */
    ball = new Sprite(200, 50, this.mBallTextureRegion, this.getVertexBufferObjectManager());
    ball.setScale(0.5f);

    final Rectangle point = new Rectangle(400, 0, 5, 5, this.getVertexBufferObjectManager());

    rope = new Line(point.getX()+5/2, point.getY()+5/2, ball.getX(), ball.getY(), this.getVertexBufferObjectManager());

    this.mScene.attachChild(ball);
    this.mScene.attachChild(rope);
    this.mScene.attachChild(point);

    final Body ballBody = PhysicsFactory.createCircleBody(this.mPhysicsWorld, ball, BodyType.DynamicBody, FIXTURE_DEF);
    final Body pointBody = PhysicsFactory.createBoxBody(this.mPhysicsWorld, point, BodyType.StaticBody, FIXTURE_DEF);

    this.mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(ball, ballBody, true, true) {

    @Override
    public void onUpdate(final float pSecondsElapsed) {
    super.onUpdate(pSecondsElapsed);
    final Vector2 movingBodyWorldCenter = ballBody.getWorldCenter();
    rope.setPosition(rope.getX1(), rope.getY1(), movingBodyWorldCenter.x * PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT, movingBodyWorldCenter.y * PhysicsConstants.PIXEL_TO_METER_RATIO_DEFAULT);
    }
    });

    final RevoluteJointDef revoluteJointDef = new RevoluteJointDef();
    revoluteJointDef.initialize(pointBody, ballBody, pointBody.getWorldCenter());
    revoluteJointDef.enableMotor = true;
    revoluteJointDef.maxMotorTorque = 1;    
    final Joint joint = this.mPhysicsWorld.createJoint(revoluteJointDef);

    //collide detector
    this.mScene.registerUpdateHandler(new IUpdateHandler() {
        @Override
        public void reset() { }

        @Override
        public void onUpdate(final float pSecondsElapsed) {
            if(rope.collidesWith(cutLine)) {
                mPhysicsWorld.destroyJoint(joint);                  
                mScene.detachChild(rope);
            } 


    }
});

    this.mScene.registerUpdateHandler(this.mPhysicsWorld);
    this.mScene.setOnSceneTouchListener(this);
    return this.mScene;
}

@Override
public boolean onSceneTouchEvent(final Scene pScene, final TouchEvent pSceneTouchEvent) {   
        if(pSceneTouchEvent.isActionDown()) {
            this.addCuter(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
            return true;
        }           
        if(pSceneTouchEvent.isActionMove()) {
            this.moveCuter(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
            return true;
        }
        if(pSceneTouchEvent.isActionUp()) {
            this.delCuter(pSceneTouchEvent.getX(), pSceneTouchEvent.getY());
            return true;
        }
    return false;}

private void addCuter(final float pX, final float pY) { 
    cutBegin = new Rectangle(pX, pY, 5, 5, this.getVertexBufferObjectManager());
    cutEnd = new Rectangle(pX, pY, 5, 5, this.getVertexBufferObjectManager());
    cutLine = new Line(cutBegin.getX()+5/2, cutBegin.getY()+5/2, cutEnd.getX(), cutEnd.getY(), this.getVertexBufferObjectManager());

    this.mScene.attachChild(cutBegin);
    this.mScene.attachChild(cutEnd);
    this.mScene.attachChild(cutLine);

    cutEnd.setColor(1, 0, 0);
    cutLine.setColor(1, 0, 0);}

private void moveCuter(final float pX, final float pY) {        
    cutEnd.setPosition(pX-5/2, pY-5/2);
    cutLine.setPosition(cutBegin.getX()+5/2, cutBegin.getY()+5/2, pX, pY);      }

private void delCuter(final float pX, final float pY) {
    this.mScene.detachChild(cutBegin);
    this.mScene.detachChild(cutEnd);
    this.mScene.detachChild(cutLine);}

错误LogCat

03-14 10:45:48.329:A / libc(12926):致命信号11(SIGSEGV)位于0x00000000(代码= 1)

1 个答案:

答案 0 :(得分:1)

我发现这通常发生在您尝试更改游戏中某些方面时,更新依赖于事件触发,即在...之外,

mActivity.runOnUpdateThread(new Runnable()
{
    @Override
    public void run()
    {
        //...                               
    }
});

你的onSceneTouchEvent看起来可能是一个有可能的竞争者!尝试在函数调用周围添加上面的代码,这些函数调用会对场景进行更改以使其“安全”,即,在更新线程处理中断时,您不会从中断更改游戏变量。