有两种基于文档的方法,它们都是鼓励松散耦合的流行 C++ 模式。
1.) 听众方法。在这种方法中,您的类(假设它称为 ObjectManager)也将“成为”,即继承自 TimeListener。这似乎是该框架希望您采用的方式。看看纯虚基类“TimeListener”
2.) 回调方法。这是对“game::Schedule”的第二次调用:http:
//gameplay3d.github.io/GamePlay/api/classgameplay_1_1_game.html#a3b8adb5a096f735bfcfec801f02ea0da
这需要一个脚本函数。我不熟悉这个框架,所以我不能评论太多,你需要传入一个指向与所需签名匹配的函数的指针
总的来说,我会做这样的事情:
class ObjectManager: public TimeListener
{
public:
void OnTimeEvent(long timeDiff, void* cookie)
{
// timeDiff is difference between game time and current time
// cookie is the data you passed into the event. it could be a pointer to anything.
// Cast appropriately. remember, it is completely optional! you can pass
// nullptr!
MyOtherObject* other = static_cast<MyOtherObject>(cookie);
// ...
// handle the event and do the other stuff I wanted to do on a timer.
}
// my other business logic and all other good stuff this class does.
private:
// data, other private things.
}
....
现在,当你想安排一个事件时,你可以安排它在你的监听器上被调用:
ObjectManager myObjectManager; // example only, stack variable. would be invalid.
// Schedule an event to be invoked on the instance noted, with a context of MyOtherObject, in 100 milliseconds.
gameplay::Game::schedule(100.0, &myObjectManager, new MyOtherObject());
您需要阅读文档以查看是否需要指向“游戏”对象的指针来调用 schedule。如果你这样做也没关系,它就像“game->Schedule(..)”那样。