2012-10-26 70 views
1

我在我的遊戲中使用Box2D,但我希望有一個很好看的彈簧牆在觸摸時推動玩家。它看起來像這樣(3幀): enter image description hereAndEngine和Box2D中的彈簧牆

問題:如何實現它?我可以將附加牆效果添加到動畫精靈嗎?

回答

1

答案是:棱柱關節。我將圖像分爲兩部分:靜態和動態(移動條)。下面的代碼是用於在我的問題中創建像圖像一樣的棱鏡聯合:

//prismatic joint 
    final Sprite springFrameT = new Sprite(pX, pY, mSpringFrameTRegion, getVertexBufferObjectManager()); 
    final Sprite springBarT = new Sprite(pX, pY + mSpringFrameTRegion.getHeight()-mSpringBarTRegion.getHeight(), 
      mSpringBarTRegion, getVertexBufferObjectManager());   
    mMainScene.attachChild(springFrameT); 
    mMainScene.attachChild(springBarT);   
    mMapSprites.add(springFrameT); 
    mMapSprites.add(springBarT); 

    final Body springFrameBody = PhysicsFactory.createBoxBody(mPhysicsWorld, springFrameT, BodyType.StaticBody, FIXTURE_DEF); 
    final Body springBarBody = PhysicsFactory.createBoxBody(mPhysicsWorld, springBarT, BodyType.DynamicBody, SPRING_FIXTURE_DEF);   
    mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(springFrameT, springFrameBody, false, false)); 
    mPhysicsWorld.registerPhysicsConnector(new PhysicsConnector(springBarT, springBarBody, true, true)); 

    final PrismaticJointDef prismaticJointDef = new PrismaticJointDef();   
    prismaticJointDef.initialize(springFrameBody, springBarBody, springFrameBody.getWorldCenter(), // new Vector2(springFrameT.getWidth(), springFrameT.getHeight()/2), 
      new Vector2(0, 1.0f)); 
    prismaticJointDef.lowerTranslation = -0.5f; 
    prismaticJointDef.upperTranslation = 0.5f; 
    prismaticJointDef.enableLimit = true; 
    prismaticJointDef.enableMotor = true; 
    prismaticJointDef.maxMotorForce = 100.0f; 
    prismaticJointDef.motorSpeed = 100000f; 
    prismaticJointDef.collideConnected = false; 
    this.mPhysicsWorld.createJoint(prismaticJointDef);