无法将setRotation设置为cocos2dx中的PhysicsBody :: createBox

时间:2017-09-03 09:28:30

标签: cocos2d-x cocos2d-x-3.0

我能够将精灵旋转90度角但无法改变物理体的角度。我的目的是使用createBlock()连接多个sprite,以创建一个可破坏的塔对象。我想将setRotation()添加到PhysicsBody :: createBox但是错误显示出来并且无法实现我想要做的事情。我搜索了一些解决方案,但无法找到解决问题的有效方法。我很想听听你的一些提示或例子!

这是创建块精灵的功能。添加了90,以便将精灵旋转到90度。

createBlock(BlockType::Block1, Point(586, 150), 90);

void GameLayer::createBlock(BlockType type, Point position, float angle)
{
    std::string fileName;

    switch (type)
    {
        case BlockType::Block1:
            fileName = "block1.png";
            break;

        case BlockType::Block2:
            fileName = "block2.png";
            break;

        case BlockType::Roof:
            fileName = "roof.png";
            break;

        default:
            fileName = "stone.png";
            break;
    }

    auto block = Sprite::create(fileName.c_str());
    block->setPosition(position);
    block->setRotation(angle);
    block->setTag(T_Block);

    PhysicsBody* body;

    switch (type)
    {
        case BlockType::Block1:
        case BlockType::Block2:
        {

            body = PhysicsBody::createBox(block->getContentSize(), PhysicsMaterial(0.5, 0.5, 0.3));
            body->setDynamic(true);
            body->setContactTestBitmask(0x01);
            break;
        }

        case BlockType::Roof:
        {
            Point points[3] = {Point(-50, -25), Point(0, 25), Point(50, -25)};
            body = PhysicsBody::createPolygon(points, 3, PhysicsMaterial(0.5, 0.5, 0.3));
            body->setDynamic(true);
            body->setContactTestBitmask(0x01);
            break;
        }

        default:
        {
            body = PhysicsBody::createBox(block->getContentSize(), PhysicsMaterial(0.5, 0.5, 0.3));
            body->setDynamic(false);
            break;
        }
    }

    block->setPhysicsBody(body);


    addChild(block, Z_Block);
}

1 个答案:

答案 0 :(得分:0)

如果您不仅要旋转精灵,还要旋转物理,我们必须在调用setRotation(angle)后调用block->setPhysicsBody(body)

所以,请修改如下代码......

1st:block->setPhysicsBody(body);

下一个:block->setRotation(angle);

最后:addChild(block, Z_Block);

我知道您的代码并且之前遇到过相同的问题!我也有这本教科书。(^ ^)