首页 > 解决方案 > Cocos2dx : 落下的物理球通过舞台

问题描述

如果我移动舞台然后球通过舞台...如果舞台不移动那么球永远不会通过舞台..如何设置球不通过?

这是代码:

ball->addComponent(PhysicsBody::createCircle(ball->getContentSize().width*0.5f, PhysicsMaterial(10.0f,0.1,0.1)));
ball->getPhysicsBody()->setGravityEnable(true);
ball->getPhysicsBody()->setRotationEnable(false);
ball->getPhysicsBody()->setDynamic(true);
ball->getPhysicsBody()->setTag(BALL_BODYS_TAG);
ball->getPhysicsBody()->setCategoryBitmask(0x01);
ball->getPhysicsBody()->setCollisionBitmask(0x02);
ball->getPhysicsBody()->setMass(10);

阶段:

stage->addComponent(PhysicsBody::createBox(stage->getContentSize(), PhysicsMaterial(100.0f,0.1,0.1)));
stage->getPhysicsBody()->setGravityEnable(false);
stage->getPhysicsBody()->setTag(STEPS_BODYS_TAG);
stage->getPhysicsBody()->setMass(100);
stage->getPhysicsBody()->setDynamic(true);

舞台移动代码:

void GBStage::updateStage(float dt)
{
    Vec2 pos = this->getPosition();

    pos.y += sBridge->gameSpeed;

    this->setPosition(pos); //Updating just sprite position

}

现在舞台向上移动,球落下。但是球通过舞台。如何在舞台上停下来。我的代码有什么问题?

在此处输入图像描述

标签: iosiphonexcodecocos2d-xgame-physics

解决方案


找到临时解决方案。现在我停止移动阶段精灵位置。我使用 setVelocity 来移动舞台。现在球从不通过台阶。

this->getPhysicsBody()->setVelocity(sBridge->steps_Velocity);

但是在带有 Box2D 的 Cocos2d-ObjC 中,我们可以选择移动精灵位置并将精灵位置设置为物理体。现在在 Cocos2d-x 中,我们不能移动 sprite 并将其位置设置为 body。如果有人知道是否可以将精灵位置设置为物理身体并仍然使用精灵实现运动,请回答。


推荐阅读