在cocos2d-x3.2中关于触摸事件的处理@H_502_1@
第一种用法 ,首先我们需要在.H文件里面定义触摸的回调,这里我们就先拿到单点触摸的3个状态把@H_502_1@
1:bool onTouchBegan(Touch * touch,Event *unused_event);//开始状态@H_502_1@
2: void onTouchMoved(Touch *touch,Event *unused_event);//移动状态@H_502_1@
3:void onTouchEnded(Touch*touch,Event *unused_event);//结束状态@H_502_1@
然后我们去.CPP里面的init()里面定义我们的触摸监听@H_502_1@
auto listener=EventlistenerTouchOneByone::create();
@H_502_1@
listener->onTouchBegan=CC_CALLBACK_2(gamescene::onTouchBegan,this);@H_502_1@
listener->onTouchMoved=CC_CALLBACK_2(gamescene::onTouchMoved,this);@H_502_1@
listener->onTouchEnded=CC_CALLBACK_2(gamescene::onTouchEnded,this);@H_502_1@
Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,this);@H_502_1@
void gamescene::onTouchBegan(Touch * touch,Event *unused_event)@H_502_1@
{@H_502_1@
log("实现触摸开始")@H_502_1@
}@H_502_1@
void gamescene::onTouchMoved(Touch*touch,Event*unused_event)@H_502_1@
{@H_502_1@
log("实现触摸移动要处理的事件")@H_502_1@
}@H_502_1@
void gamescene::onTouchEnded(Touch *touch,Event*unused_event)@H_502_1@
{@H_502_1@
log("实现触摸结束后的操作")@H_502_1@
}@H_502_1@
这是第一种办法,然后我们来说第2种比较简单的办法@H_502_1@
我们直接在 。cpp里面直接写触摸事件写在init()方法里面@H_502_1@
auto listener=EventListenerTouchOneByone::create();@H_502_1@
listener->onTouchBegan=[&](Touch *touch,Event*event)->bool{@H_502_1@
log("这里就可以写你需要触摸时的事件了")@H_502_1@
}@H_502_1@
listener->onTouchEnded=[&](Touch *touch,Event*event)@H_502_1@
{@H_502_1@
log("触摸移动")@H_502_1@
}@H_502_1@
listener->onTouchEnded=[&](Touch *touch,Event*event){@H_502_1@
log("触摸结束")@H_502_1@
}
@H_502_1@
//加载触摸@H_502_1@
this->getEventDispatcher()->addEventListenerWithSceneGraPHPriority(listener,this);@H_502_1@