Cocos2dx 3.2 Assert Failed:Layer#ccTouchesBegan覆盖了我

时间:2014-08-09 01:56:20

标签: android c++ cocos2d-x cocos2d-x-3.0

我想启用触摸但是当我触摸我的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");
}

1 个答案:

答案 0 :(得分:0)

问题可能在:

listener->onTouchBegan = CC_CALLBACK_2(HelloWorld::onTouchBegan, this);

void HelloWorld::onTouchesBegan(cocos2d::Touch *pTouches, cocos2d::Event *pEvent)

您的函数名称onTouchBeganonTouchesBegan不匹配。

当我创建一个继承自LayerColor的图层时,我遇到了同样的问题。显示了相同的错误消息,但是当我将继承从LayerColor更改为Layer时,它工作了!我用过cocos2dx 3.6。