2015-07-28 159 views
0

我爲uiButton的創建創建了類,有三個函數如下; 我創建了按鈕類,下面有3個函數類;addTouchEventListener的參數是什麼

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?我查看了庫中的參數,但它不起作用。

例如從我的gamescene創建按鈕;

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 *' 

回答

1

我認爲你對你的情況使用了錯誤的過載。您需要使用void addTouchEventListener (const ccWidgetTouchCallback &callback)過載。 這不起作用的原因是因爲沒有回調綁定到myButton。您需要綁定一個自定義的回調:

假設你的按鈕類從::cocos2d::ui::Button::cocos2d::ui::Widget繼承,你需要分配一個回調函數 的Widget::addTouchEventListener()方法。當接收到用於該按鈕的TouchEvent

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

回調MyButtonClass::touchEvent將被調用。

例如:

EDIT

另一種方法是通過設置以下的一個或多個使用中myButton方法:

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

檢查出codos2d -x doc ui::Button class 還有一個例子onTouchBeganonTouchEndedhere。 在此示例中,您可以使用myButton而不是listener1

+0

那麼如何在我的函數中使用'ccWidgetTouchCallback'參數。我的類不是從':: cocos2d :: ui :: Button'或':: cocos2d :: ui :: Widget'繼承的。 –

+0

您可以在'myButton'對象上調用'addTouchEventListener'。你只是在'myButton'中使用'addTouchEventListener'的不正確的重載。回調可以綁定到任何東西,包括你自己班級的方法。 – namezero

+0

我已經知道了,如何設置其他課程的活動?我的uiButton在myButtonClass中是私有的。我的SetTouchListener方法是公開的,我想爲此方法設置自定義回調。 –