我怎样才能设置 body 在行动

How can I set body in action

我想在移动延迟后设置 body,我正在 sprite-kit 中搜索类似 runBlock 的内容。

void MySprite::SpawnSprite( cocos2d::Layer *layer )
{
    auto mySprite = Sprite::create();
    auto body = PhysicsBody::create();

    mySprite->setTexture("MySprite.png");        
    body->createCircle(arrow->getContentSize().width / 2);
    body->setDynamic(false);
    mySprite->setPosition( startPoint );
    layer->addChild(mySprite);

    auto moveTest = MoveTo::create(2, Point(200, 200) );
    auto waitAction = DelayTime::create(2);
    auto action = Sequence::create(moveTest, waitAction, NULL);//I want to set body after waitAction in sequence(mySprite->setPhysicsBody(body))
    mySprite->runAction(action);
}

这在sprite-kit

中是如此简单
runAction(
    SKAction.sequence([
        action,
        SKAction.waitForDuration(2.0),
        SKAction.runBlock({ //Set Body }))
        ])
    )

您需要创建 CallFuncN 并像这样在您的序列中使用它

拳头一个你需要的功能:

void attachBody(Ref* pSender)
{
Sprite* mySprite = (Sprite*)pSender;
mySprite->setPhysiceBody(body);
//you can do whatever you want to do with your sprite here.
}

现在你的序列应该是这样的

auto action = Sequence::create(moveTest, waitAction,CallFuncN::create(CC_CALLBACK_1(attachBody,this)), NULL);
void MySprite::SetBody(Sprite *sprite)
{
    auto body = PhysicsBody::create();
    body->createCircle(arrow->getContentSize().width / 2);
    body->setDynamic(false);
    sprite->setPhysicsBody(body);
}

void MySprite::SpawnSprite( cocos2d::Layer *layer )
{
    auto mySprite = Sprite::create();

    mySprite->setTexture("MySprite.png");        
    mySprite->setPosition( startPoint );
    layer->addChild(mySprite);

    auto moveTest = MoveTo::create(2, Point(200, 200) );
    auto waitAction = DelayTime::create(2);
    auto funcCallAction = CallFunc::create([=](){
            MySprite::SetBody(mySprite);
        });
    auto action = Sequence::create(moveTest, waitAction, funcCallAction, NULL);
    mySprite->runAction(action);
}