我正在尝试使用 cocos2d-x
编写一些游戏,并且我正在尝试定义一些自定义事件,例如 on_game_start
、on_game_end
> 等等。我搜索了论坛并谷歌搜索了网络,似乎没有太多提及这一点。那么,有人请给我一些建议吗?谢谢。
最佳答案
cocos2d-x中好像没有全局事件之类的东西。所以我尝试使用 CCNotificationCenter 作为替代方案。
这是我使用的代码:
//Send event.
CCNotificationCenter::sharedNotificationCenter()->postNotification(EMEMY_KILLED_MSG, (CCObject*)data);
//Listen event.
void GameManager::initListener()
{
CCNotificationCenter::sharedNotificationCenter()->addObserver(this, callfuncO_selector(GameManager::onEnemyKilled), CLICK_TEST_MSG, NULL);
}
//Handling event
void GameManager::onEnemyKilled(CCObject* obj)
{
CCMessageBox("onEnemyKilled", "Title");
//Remove event listener.
CCNotificationCenter::sharedNotificationCenter()->removeObserver(this, EMEMY_KILLED_MSG);
}
关于cocos2d-x - 使用 cocos2dx 自定义事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16703361/