http://blog.csdn.net/lnb333666/article/details/16857637
- //触屏事件
- virtualboolccTouchBegan(Touch*touch,Event*event);
- virtualvoidccTouchMoved(Touch*touch,Event*event);
- virtualvoidccTouchEnded(Touch*touch,226); color:inherit; line-height:18px">
- //生命周期
- virtualvoidonEnter();
- virtualvoidonExit();
- voidTouchdemo::onEnter()
- {
- //开启触屏监听
- Director::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,false);
- Layer::onEnter();//一定不要忘了
- }
- voidTouchdemo::onExit()
- {
- //关闭触屏监听
- Director::sharedDirector()->getTouchDispatcher()->removeDelegate(this);
- Layer::onExit();//一定不要忘了
- }
- ===========================================================================================
- 添加委托
- addStandardDelegate();
- 开启多点触控(不写没有效果哦)
- setTouchEnabled();
- 触屏事件:
- 1.注册多点触控
- virtualvoidregisterWithTouchDispatcher(void);
- 2.当用户第一次触碰手机屏幕时响应的回调函数
- virtualvoidccTouchesBegan(Set*touchs,85); line-height:18px"> 3.当用户手指在手机屏幕上滑动时响应的回调函数
- virtualvoidccTouchesMoved(Set*touchs,226); color:inherit; line-height:18px"> 4.当用户手指在离开手机屏幕上时响应的回调函数
- virtualvoidccTouchesEnded(Set*touchs,0); background-color:inherit">//写上多点触控的回调函数
- virtualvoidccTouchesBegan(Set*set,Event*e);
- virtualvoidccTouchesMoved(Set*set,Event*e);
- virtualvoidccTouchesEnded(Set*set,0); background-color:inherit">//写上生命周期函数
- 注册监听
- voidTouchsdemo::registerWithTouchDispatcher(void)
- //注册监听
- Director::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);
- voidTouchsdemo::ccTouchesBegan(Set*set,Event*e)
- SetIteratoriter=set->begin();
- //这里的iter相当于系统分配的数字,
- //set->begin()获取第一个数字
- //set->end()就是最后一位
- for(;iter!=set->end();iter++){
- //这里呢,就是单点处理了,可以理解为把多点拆成单点来做
- CTouch*mytouch=(Touch*)(*iter);
- Pointcp=mytouch->getLocation();
- //这里我们对5个触摸块进行处理
- if(mytouch->getID()==0){
- Sprite*sp=(Sprite*)this->getChildByTag(1);
- sp->setPosition(cp);
- if(mytouch->getID()==1){
- Sprite*sp=(Sprite*)this->getChildByTag(2);
- if(mytouch->getID()==2){
- Sprite*sp=(Sprite*)this->getChildByTag(3);
- if(mytouch->getID()==3){
- Sprite*sp=(CCSprite*)this->getChildByTag(4);
- if(mytouch->getID()==4){
- Sprite*sp=(Sprite*)this->getChildByTag(5);
- voidTouchsdemo::ccTouchesEnded(Set*set,CCEvent*e)
- CCSetIteratoriter=set->begin();
- inta[6]={0,0};//创建一个数组
- Touch*mytouch=(Touch*)(*iter);
- Pointcp=mytouch->getLocation();
- //这里我们对5个触摸块进行处理
- if(mytouch->getID()==0){
- a[1]=1;
- a[2]=1;
- if(mytouch->getID()==2){
- a[3]=1;
- a[4]=1;
- if(mytouch->getID()==4){
- a[5]=1;
- for(inti=1;i<=5;i++){<>
- if(a[i]==1){
- Sprite*sp=(Sprite*)this->getChildByTag(i);
- sp->runAction(MoveTo::create(5,ccp(30,30)));
- //实现生命周期函数
- voidTouchsdemo::onEnter()
- Layer::onEnter();
- voidTouchsdemo::onExit()
- Layer::onExit();
- }