0

有人可以写一个关于如何在 Gameplay3D 中使用此功能的具体示例:

virtual void gameplay::TimeListener::timeEvent  (   long    timeDiff,
void *  cookie 
)       [pure virtual]

我的意思是想在 t 毫秒后调用一个函数,但我不确定我应该如何编写代码。

这是文档: http: //gameplay3d.github.io/GamePlay/api/classgameplay_1_1_time_listener.html

4

1 回答 1

0

有两种基于文档的方法,它们都是鼓励松散耦合的流行 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(..)”那样。

于 2016-06-08T04:46:37.663 回答