【已解决】触摸事件出现未知异常

edolele
Mon, 28 Jul 2014 12:29:33 +0000

各位大神，我参照公开课第26集中写了触摸事件的代码，但是出现了下面错误：

错误出现在CCobject.cpp中的CCAssert(m_uReference > 0, “reference count should greater than 0”);

主程序MyScene.cpp中的CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);

MyScene.cpp中设置触摸事件的代码如下

void MyScene::onEnter(){
    CCLayer::onEnter();
    //注册触摸事件
    CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);
    //参数3是否继续低优先级操作
}

void MyScene::onExit(){
    CCLayer::onExit();
    CCDirector::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
}

bool MyScene::ccTouchBegan(CCTouch *pTouch, CCEvent *event){
    CCLOG("touchbegan");

    return true;//true表示还响应后面的其他响应函数，false不响应后面的操作
}
void MyScene::ccTouchMoved(CCTouch *pTouch, CCEvent *event){
    CCLOG("touchmoved");
}
void MyScene::ccTouchEnded(CCTouch *pTouch, CCEvent *event){
    CCLOG("touchended");
}
void MyScene::ccTouchCancelled(CCTouch *pTouch, CCEvent *event){
    CCLOG("touchended");
}

MyScene.h中声明函数如下:

virtual void onEnter();
virtual void onExit();

virtual bool ccTouchBegan(CCTouch *pTouch, CCEvent *event);//第一次碰到屏幕时响应
virtual void ccTouchMoved(CCTouch *pTouch, CCEvent *event);//滑动操作
virtual void ccTouchEnded(CCTouch *pTouch, CCEvent *event);//离开屏幕
virtual void ccTouchCancelled(CCTouch *pTouch, CCEvent *event);//取消操作

阅读整个主题