Gameplay3D gameplay::TimeListener
Gameplay3D gameplay::TimeListener
谁能写一个关于如何在 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
基于文档有两种方法,它们都是鼓励松散耦合的流行 C++ 模式。
1.) 倾听者的方法。在这种方法中,您的 class(假设它称为 ObjectManager)也将 'be' 即继承自 TimeListener。这似乎是该框架希望您采用的方式。看看纯虚基地class"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());
您需要阅读文档以查看是否需要指向 "Game" 对象的指针来调用调度。如果你这样做并不重要,就像 "game->Schedule(..)" 那样。
谁能写一个关于如何在 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
基于文档有两种方法,它们都是鼓励松散耦合的流行 C++ 模式。
1.) 倾听者的方法。在这种方法中,您的 class(假设它称为 ObjectManager)也将 'be' 即继承自 TimeListener。这似乎是该框架希望您采用的方式。看看纯虚基地class"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());
您需要阅读文档以查看是否需要指向 "Game" 对象的指针来调用调度。如果你这样做并不重要,就像 "game->Schedule(..)" 那样。