我想启用触摸但是当我触摸我的Android设备时断言失败:图层#ccTouchesBegan覆盖我显示。
Hello World初始化方法:
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
Size visibleSize = Director::getInstance()->getWinSize();
Sprite* bg = Sprite::create("myBg.png");
bg->setPosition(visibleSize.width/2, visibleSize.height/2);
float rX = visibleSize.width / bg->getContentSize().width;
float rY = visibleSize.height / bg->getContentSize().height;
this->addChild(bg, -1);
bg->setScale(rX, rY);
//this->setTouchEnabled(true);
auto listener = EventListenerTouchOneByOne::create();
listener->setSwallowTouches(true);
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
_eventDispatcher->addEventListenerWithSceneGraphPriority(listener, this);
return true;
}
onTouchBegan方法:
void HelloWorld::onTouchesBegan(cocos2d::Touch *pTouches, cocos2d::Event *pEvent)
{
CCLog("hello");
}
答案 0 :(得分:0)
问题可能在:
listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);
和
void HelloWorld::onTouchesBegan(cocos2d::Touch *pTouches, cocos2d::Event *pEvent)
您的函数名称onTouchBegan
和onTouchesBegan
不匹配。
当我创建一个继承自LayerColor的图层时,我遇到了同样的问题。显示了相同的错误消息,但是当我将继承从LayerColor更改为Layer时,它工作了!我用过cocos2dx 3.6。