我試圖實現一種簡單的方法來在我的類中啓用和禁用Touch監聽器。我試着寫我的類中的方法:在Cocos 2D-X中添加和刪除事件監聽器3.2
void HelloWorld::setTouchEnabled(bool enabled)
{
if (enabled)
{
auto _touchListener = EventListenerTouchAllAtOnce::create();
_touchListener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(_touchListener, this);
}
else if (!enabled)
{
_eventDispatcher->removeEventListener(_touchListener);
}
}
我希望能夠再調用setTouchEnabled(true)
或setTouchEnabled(false)
從這個類中的任何其他方法中。但是,由於_touchListener
在函數結束時被釋放,所以這不起作用。當我試圖在我的頭文件中聲明EventListener *_touchListener
,我在XCode中收到錯誤在這條線:
_touchListener->onTouchesBegan = CC_CALLBACK_2(HelloWorld::onTouchesBegan, this);
錯誤說,沒有成員名爲onTouchesBegan
存在cocos2d::EventListener
。
我假設必須有一個簡單的方法來做到這一點。
看看這個:http://www.cocos2d-x.org/wiki/How_To_Subclass_Sprite_And_Add_Event_Listeners。我想你會因爲某些類的重命名而在3.2版本中遇到一些編譯錯誤,但這些很容易修復。 – GameDeveloper 2014-08-29 15:38:33