0

请帮忙!删除 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);}

错误日志猫

03-14 10:45:48.329: A/libc(12926): 致命信号 11 (SIGSEGV) 在 0x00000000 (code=1)

4

1 回答 1

1

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

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

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

于 2013-03-15T22:12:19.260 回答