Cocos2d x 我怎样才能使身体起作用

Cocos2d x 我怎样才能使身体起作用,cocos2d-x,cocos2d-x-3.0,Cocos2d X,Cocos2d X 3.0,我想在移动延迟后设置身体,我正在精灵套件中搜索类似runBlock的东西 void MySprite::SpawnSprite( cocos2d::Layer *layer ) { auto mySprite = Sprite::create(); auto body = PhysicsBody::create(); mySprite->setTexture("MySprite.png"); body->createCircle(ar

我想在移动延迟后设置身体,我正在精灵套件中搜索类似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);
}
这是如此简单的雪碧套件

runAction(
    SKAction.sequence([
        action,
        SKAction.waitForDuration(2.0),
        SKAction.runBlock({ //Set Body }))
        ])
    )

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

auto action = Sequence::create(moveTest, waitAction,CallFuncN::create(CC_CALLBACK_1(attachBody,this)), NULL);
首先是您需要的功能:

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);

但是我应该在我的SpawnSprite函数中创建sprite和body,因为我有很多相同的sprite。如果我没有在函数中创建所有的精灵都会受到我的操作的影响,对吗?你想将主体附加到精灵上,在它上运行序列操作吗?是的,也许我可以使用对象池来实现。我试试看。