2013-03-14 271 views
0

請幫助!移除碰撞後發生碰撞後,我的遊戲崩潰。AndEngine Box2D。移除碰撞後的碰撞遊戲碰撞

遊戲是掛在繩子上的身體。手指割斷繩子,遊戲崩潰!

我的代碼:

@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

10月3日至14日:45:48.329:A/libc的(12926):致命信號11(SIGSEGV)在00000000(代碼= 1)

回答

1

我發現,當你試圖改變遊戲的更新是依賴於從事件觸發,即,外部的一些方面,這通常發生,

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

您的onSceneTouchEvent看起來像它可能是一個可能的競爭者!嘗試在函數調用周圍添加上面的代碼,這些代碼會對場景進行更改以使其「安全」,即在更新線程正在處理中斷時不會更改來自中斷的遊戲變量。