i am getting error that ‘Object’ is deprecated…should i use Node instead ?
you should be using Ref
instead of Object
. Please see: https://github.com/cocos2d/cocos2d-x/blob/cocos2d-x-3.0/docs/RELEASE_NOTES.md#user-content-object-is-replaced-with-ref
For v3, I am actually using lambdas a lot.
this->setCallback([&](cocos2d::Ref *sender)
{
Utils::LogText("clicked order complete button");
SingleOrderMenuItem::completeOrder(getTag());
});
k… thats abt the Object…
I’m interested in this as well. I use factories to build my buttons and I used to store SEL_MenuHandler and CCNode and build the callback that way while looping through my button-builder.
i hav dis problem frm last 2 days…and no answer still
menu_selector() is now defined with a static_cast of SEL_MenuHandler as you’ve noticed with your compiler error.
In 2.x you could declare any subclass of Object* to refer to the sender, for example:
void myMenuCallback(MenuItemSprite* sender);
void MyClass::myMenuCallback(MenuItemSprite* sender) { // menu item activated }
// creating menu item with callback
CCMenuItemSprite::create(normal, selected, this, menu_selector(MyClass::myMenuCallback);
With 3.x the static_cast your callback must be declared like:
void myMenuCallback(Ref* sender);
void MyClass::myMenuCallback(Ref* sender) {
MenuItemSprite* menuItem = static_cast<MenuItemSprite*>(sender);
}
// creating menu item with callback
CCMenuItemSprite::create(normal, selected, this, menu_selector(MyClass::myMenuCallback);
You can also use a lamdda as @slackmoehrle mentioned, or you can use the new CC_CALLBACK_1() macro where the 1 refers to the callback having 1 bound parameter. This uses the new std::bind and std::function of c++11 which is effectively the same as using a lambda, but with the callback function defined elsewhere instead of inline within the menu item create call.
void myMenuCallback(Ref* sender);
void MyClass::myMenuCallback(Ref* sender) { {
MenuItemSprite* menuItem = static_cast<MenuItemSprite*>(sender);
}
// creating menu item with callback
MenuItemSprite *menuItem = MenuItemSprite::create(normal, selected, CC_CALLBACK_1(MyClass::myMenuCallback, this));
@stevetranby Thanks Mate…
@slackmoehrle sorry for not understanding your answer,
bdw @stevetranby had a better explanation !
I’m glad his answer helped you. There are a few different ways to accomplish what you want.