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

edolele
Tue, 29 Jul 2014 03:20:01 +0000

MyScene.h

#pragma once
#include "cocos2d.h"

using namespace cocos2d;


class MyScene : CCLayer
{
public:
    MyScene(void);
    ~MyScene(void);
    static CCScene* scene();
    virtual bool init();

    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);//取消操作

    CREATE_FUNC(MyScene);
};

MyScene.cpp

#include "MyScene.h"
MyScene::MyScene(void)
{
}


MyScene::~MyScene(void)
{
}

CCScene* MyScene::scene(){
    // 'scene' is an autorelease object
    CCScene *scene = CCScene::create();

    // 'layer' is an autorelease object
    MyScene *layer = MyScene::create();

    // add layer as a child to scene
    scene->addChild(layer);
    // return the scene
    return scene;
}

bool MyScene::init(){

    return true;
}

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");
}

谢谢了！！

阅读整个主题