使用日程表功能更新精灵位置

update sprite position with schedule function

本文关键字:精灵 位置 更新 功能 日程表      更新时间:2023-10-16

在我的游戏中,我有一个动画精灵。我在游戏中添加了box2d来增加重力。现在我想使用日程表功能来更新精灵的位置。我用objective-C实现了它,但我希望它是一个多平台游戏,所以现在我正在C++中尝试它。

在我的init中,我有以下行:

this->schedule(cocos2d::SEL_SCHEDULE(View::tick(1.0f)));

方法勾选:

void View::tick(float dt) {
world->Step(dt, 10, 10);
for(b2Body *b = world->GetBodyList(); b; b=b->GetNext()) {
if(b->GetUserData() != NULL) {
}
}
}

我在init中的行中收到以下错误:"无法从类型'void'转换为成员指针类型'coco2d::sel_schedule'(又名void coco2d:::CCobject::*)(float)

我不知道现在该怎么办。我试着在谷歌上搜索,但每个人都使用objective-c或schedule_selector(显然已经不起作用了)

提前感谢

您可能不应该使用schedule函数来更新单个sprite。

在您的场景类中,覆盖虚拟方法进行更新(…):

virtual void update(float dt)
{
    CCLOG("This was called.");
    // Do updates for everything in the scene you need to call on a tick.
}

此外,覆盖onEnter()和onExitTransitionDidStart()。在第一个步骤中,调用scheduleUpdate来设置场景,以便在刻度上自动调用。在第二个步骤中,调用unscheduledUpdate(在场景退出时)以停止更新。

void IntroScene::onEnter()
{
   CCScene::onEnter();
   scheduleUpdate();
}
void IntroScene::onExitTransitionDidStart()
{
   CCScene::onExitTransitionDidStart();
   unscheduleUpdate();
}

这样做可以让您明确控制何时更新。。。在更新周期中只有一个函数被调用。

但是,如果你真的想这么做:

像这样声明您的自定义更新程序:

void IntroScene::myUpdate(float dt)
{
   CCLOG("Also called");
}

安排如下:

schedule(schedule_selector(IntroScene::myUpdate), 1.0f);

这样安排:

unschedule(schedule_selector(IntroScene::myUpdate));

**注意**据我所知,您只能"调度"从CCNode派生的对象。

这有帮助吗?