cocos2d-x - 使用 cocos2dx 自定义事件

标签 cocos2d-x

我正在尝试使用 cocos2d-x 编写一些游戏,并且我正在尝试定义一些自定义事件,例如 on_game_starton_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/

相关文章:

c++ - CCPoint 到 float 转换

c++ - Cocos2d/Cocos2d-x粒子系统暂停

android - 在 Windows 8 上的 cocos 2dx 游戏开发环境设置中出现错误

c++ - cocos2dx 检测与多边形 Sprite 的交集

windows - Android 库链接和 LOCAL_SRC_FILES 指向丢失的文件

c++ - 如何在点击时更改 Sprite 图像

Android 后退按钮在 Cocos2d-x 中不起作用

android - Android Gradle构建失败-未能使用哈希字符串 'android-22'找到目标

android - 如何创建Cocos2d-x v3.0 Beta 2 android ndk项目

android - 汇编程序消息 : Branch out of range