addTouchEventListener的参数是什么

What are the parameters of addTouchEventListener

我创建了class用于uiButton的创建,有以下三个功能; 我创建了按钮 class class 中有 3 个函数,如下所示;

void MyButtonClass::Create(const std::string &buttonImage, cocos2d::Layer *layer)
{
    myButton = ui::Button::create(buttonImage, buttonImage);
    layer->addChild(myButton, 100);
}

void MyButtonClass::SetPosition(float xPosition, float yPosition)
{
    myButton->cocos2d::Node::setPosition(xPosition, yPosition);
}

 void MyButtonClass::SetTouchListener(Ref *sender, SEL_TouchEvent *selector)//There is problem
{
    myButton->addTouchEventListener(sender, selector);
}

如何设置TouchListener?我从库中查看了参数,但它不起作用。

例如从我的游戏场景创建按钮;

button.Create("Play.png", this);
button.SetPosition(100, 200);
button.SetTouchListener(CC_CALLBACK_1(GameScene::Play, this));//Exception: No viable conversion from '__bind<void (GameScene::*)(cocos2d::Ref *), GameScene *, std::__1::placeholders::__ph<1> &>' to 'cocos2d::Ref *'

我认为您针对您的情况使用了错误的重载。您需要使用 void addTouchEventListener (const ccWidgetTouchCallback &callback) 重载。 这不起作用的原因是因为没有回调绑定到 myButton。您需要绑定自定义回调:

假设您的按钮 class 继承自 ::cocos2d::ui::Button::cocos2d::ui::Widget,您需要分配一个回调 Widget::addTouchEventListener() 方法。

myButton->addTouchEventListener(this, toucheventselector(MyButtonClass::touchEvent));

回调 MyButtonClass::touchEvent 将在收到按钮的触摸事件时调用。

例如:

void MyButtonClass::SetTouchListener(Ref *sender, SEL_TouchEvent *selector)//There is problem
{
    myButton->addTouchEventListener([](Ref*, Widget::TouchEventType){
        // Simple callback for touch
        int n = 0;
    });
}

编辑

另一种方法是通过设置以下一项或多项来使用 myButton 中的方法:

onTouchBegan (Touch *touch, Event *unusedEvent)
onTouchMoved (Touch *touch, Event *unusedEvent)
onTouchEnded (Touch *touch, Event *unusedEvent)
onTouchCancelled (Touch *touch, Event *unusedEvent)

查看 ui::Button class 的 codos2d-x 文档 还有onTouchBeganonTouchEndedhere的例子。 在此示例中,您可以使用 myButton.

而不是 listener1