有人能写一个关于如何在Gameplay3D中使用这个函数的具体示例吗?
virtual void gameplay::TimeListener::timeEvent ( long timeDiff,
void * cookie
) [pure virtual]我的意思是,a想在t毫秒之后调用一个函数,但我不知道该如何编写代码。
以下是文档:listener.html
发布于 2016-06-08 04:46:37
有两种基于文档的方法,这两种方法都是鼓励松散耦合的流行C++模式。
1.)听者的方法。在这种方法中,您的类(假设它被称为ObjectManager)也将是TimeListener继承的。这似乎就是这个框架想要你去的方式。查看纯虚拟基类"TimeListener“
2.)回调方法。这是对“game::Schedule”的第二个调用: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());您将需要读取文档,以查看是否需要指向“游戏”对象的指针来调用计划。如果你这么做并不重要,就像“游戏->时间表(.)”取而代之的是。
https://stackoverflow.com/questions/37692707
复制相似问题