Hi,
I have a class called “MessageBoxYesNo”. And this class has two buttons. I want to bind touch event listener functions to these two buttons but the functions should be in another class.
I mean;
The class including my buttons: MessageBoxYesNo
The class including callback functions: HelloWorld
For example in my MessageBoxYesNo class, I have followings:
sprButtonYes = ui::Button::create("btnNormal.png", "btnHover.png");
sprButtonNo = ui::Button::create("btnNormal.png", "btnHover.png");
sprButtonYes->setTouchEnabled(true);
sprButtonNo->setTouchEnabled(true);
sprButtonYes->addTouchEventListener(CC_CALLBACK_2(HelloWorld::buttonYes1Pressed, this));
sprButtonNo->addTouchEventListener(CC_CALLBACK_2(HelloWorld::buttonNo1Pressed, this));
I defined buttonYes1Pressed and buttonNo1Pressed in my “HelloWorldScene.cpp”. But it doesn’t recognize. I also tried including “HelloWorldScene.h” now it gives me error (std::invoke bla bla). I don’t know what to do please help me, thanks.
Why not just extend TouchEventListener
to make your own touch events and then you can add them to as many classes as you want.
class myTouchEvent : public cocos2d:CCEventTouch
{
}
look at CCEventTouch.h
for what needs implementing.
1 Like
Sorry I still don’t understand. What should I change inside my own touch event class? What is the point of extending TouchEventListener. I don’t get it
What you want to do is a callback?
sample:
class MessageBoxYesNo : public cocos2d::Node {
public:
...
void setClickYesButtonCallback(const std::function<void()>& callback);
void setClickNoButtonCallback(const std::function<void()>& callback);
private:
...
std::function<void()> mClickYesCallback;
std::function<void()> mClickNoCallback;
bool init() override;
}
MessageBoxYesNo::MessageBoxYesNo() :
mClickYesCallback(nullptr),
mClickNoCallback(nullptr)
{
}
MessageBoxYesNo::setClickYesButtonCallback(const std::function<void()>& callback) {
mClickYesCallback = callback;
}
MessageBoxYesNo::setClickNoButtonCallback(const std::function<void()>& callback) {
mClickNoCallback = callback;
}
bool MessageBoxYesNo::init() {
if(!Node::init()) {
return false;
}
...
sprButtonYes = ui::Button::create("btnNormal.png", "btnHover.png");
sprButtonNo = ui::Button::create("btnNormal.png", "btnHover.png");
...
addChild(sprButtonYes);
addChild(sprButtonNo );
sprButtonYes->addClickEventListener([this](Ref*) {
if(mClickYesCallback) {
mClickYesCallback();
}
});
sprButtonNo->addClickEventListener([this](Ref*) {
if(mClickNoCallback) {
mClickNoCallback();
}
});
return true;
}
void HelloWorld::showMessageBox() {
auto box = MessageBoxYesNo::create();
box->setClickYesButtonCallback([this] {
CCLOG("click yes");
});
box->setClickNoButtonCallback([this] {
CCLOG("click no");
});
addChild(box);
}
Doesn’t it sort of break encapsulation calling events from some other class inside the class you really want them in?
You are right but I’m unfamiliar with listeners, events and classes so I’m unable to create my own touch class and I’m unable to change it as I want.