首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Gameplay3D游戏::TimeListener

Gameplay3D游戏::TimeListener
EN

Stack Overflow用户
提问于 2016-06-08 03:24:46
回答 1查看 83关注 0票数 0

有人能写一个关于如何在Gameplay3D中使用这个函数的具体示例吗?

代码语言:javascript
复制
virtual void gameplay::TimeListener::timeEvent  (   long    timeDiff,
void *  cookie 
)       [pure virtual]

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

以下是文档:listener.html

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-06-08 04:46:37

有两种基于文档的方法,这两种方法都是鼓励松散耦合的流行C++模式。

1.)听者的方法。在这种方法中,您的类(假设它被称为ObjectManager)也将是TimeListener继承的。这似乎就是这个框架想要你去的方式。查看纯虚拟基类"TimeListener“

2.)回调方法。这是对“game::Schedule”的第二个调用:game.html#a3b8adb5a096f735bfcfec801f02ea0da --这是一个脚本函数。我不太熟悉这个框架,所以我不能对它进行过多的评论,您需要传入一个指向与所需签名匹配的函数的指针

总的来说,我会这样做:

代码语言:javascript
复制
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.
}

....

现在,当您想要安排一个事件时,您可以安排在侦听器上调用它:

代码语言:javascript
复制
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());

您将需要读取文档,以查看是否需要指向“游戏”对象的指针来调用计划。如果你这么做并不重要,就像“游戏->时间表(.)”取而代之的是。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37692707

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档