c++ - cocos2d-x菜单项错误带回调函数

标签 c++ cocos2d-x

我有这段 5 年前的代码:

CCMenuItemSprite* quitItem = CCMenuItemSprite::create(quitLabel,quitLabel2,quitLabel3,this,menu_selector(ClearScene::call_quitItem));

我正在重构代码并迁移到最新的 cocos2d-x。上面的例子一直给我错误:

error: invalid static_cast from type 'void (MyScene::*)()' to type 'cocos2d::SEL_SCHEDULE {aka void (cocos2d:Ref::*)(float)}'

我已经尝试使用 CC_CALLBACK_n 但仍然无法正常工作。 (类似于HelloWorld.cpp 文件:

auto closeItem = MenuItemImage::create("CloseNormal.png","CloseSelected.png",CC_CALLBACK_1(HelloWorld::menuCloseCallback, this));

最佳答案

call_quitItem 方法中有多少个参数?

CC_CALLBACK_N

(N)number表示回调函数有多少个参数。 CC_CALLBACK_0表示没有入参的回调函数,比如MyClass::doSomething()

CC_CALLBACK_1 表示只有 1 个输入参数。例如,MyClass::doSomething(int a)


// new callbacks based on C++11
#define CC_CALLBACK_0(__selector__,__target__, ...) std::bind(&__selector__,__target__, ##__VA_ARGS__)
#define CC_CALLBACK_1(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, ##__VA_ARGS__)
#define CC_CALLBACK_2(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, ##__VA_ARGS__)
#define CC_CALLBACK_3(__selector__,__target__, ...) std::bind(&__selector__,__target__, std::placeholders::_1, std::placeholders::_2, std::placeholders::_3, ##__VA_ARGS__)

关于c++ - cocos2d-x菜单项错误带回调函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47880740/

相关文章:

c++ - 为什么我的专用模板函数 'OverloadedFunk' 在 C++ 中给出 "could not be resolved"错误?

c++ - 为什么我不能在 Exception 类中有 auto_ptr

ios - 将 NSString 转换为 Cocos2d-x CCString

android - 无法设置 Cocos2dx [Android、Eclipse、Windows]

c++ - 是否可以使用 cmake 为没有 pod 的 Xcode 11 链接 xcframework (FirebaseCore.xcframework)?我找不到例子

c++ - 如何在 Cocos2dx 中为 ProgressTimer 的 Sprite 添加标签?

c++ - 错误 C1189 MFC

c++ - 分配空间中的链表?

c++ - 不要将特定项目存储在数组 C++ 中

c++ - Cocos2d-x c++ - 在继续之前等待 Action 完成