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