How to use Ref * pSender

How to use Ref * pSender
0.0 0

#1

I’ve read online that Ref * pSender is used as a pointer to an object that calls a function, but I haven’t been able to figure out how to use it.

Here’s an example. I create a UI Button here.

playButton->addTouchEventListener(CC_CALLBACK_2(HelloWorld::playEvent, this));

And this is the function that is run when the button is pressed;

void HelloWorld::onTouchEnded(cocos2d::Touch *touch, cocos2d::Event *event)
{
    //Do stuff here
}

How would I use the Ref * pSender to get a pointer to the playButton object?


#2
auto button = dynamic_cast<ui:Button*>(pSender);

Turns out you have to use the above code. You must cast Ref * pSender.


#3

@DJEclipse , but what are some usages of it? Just treat it as a normal Node likes button->setPoisition or button->runAction ?


#4

Yea. You would just use it the same way you would use any object of that type.

In my case, pSender was the same as playButton. They were both pointers to the same thing. I just couldn’t use pSender because the compiler didn’t know that Ref * pSender was a ui::Button. I simply had to explicitly cast it to ui::Button so that the compiler would understand and let me do what I wanted to do.