单点触摸实例:主角随手指移动
首先在布景层的init函数中新建主角和加入触屏检测:
bool HelloWorld::init(){
if(!CCLayer::init()){
return false;
}
setTouchEnabled(true);
// ...中间省略代码为HelloWorld代码
hero = CCSprite::create("grossini.png");
hero->setPosition(ccp(size.width/2,size.height/2));
this->addChild(hero,0);
return true;
}
然后重写registerWithTouchDispatcher等5个函数:
void HelloWorld::registerWithTouchDispatcher(){
CCDirector* pDirector = CCDirector::sharedDirector();
pDirector->getTouchDispatcher()->addTargetedDelegate(this,0,true);
}
bool HelloWorld::ccTouchBegan(CCTouch* touch,CCEvent* event){
CCPoint heropos = hero->getPosition();
CCPoint location = touch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
if(location.x>heropos.x-42.5&&location.x<heropos.x+42.5&&location.y>heropos.y-60.5&&location.y<
heropos.y+60.5){
isControl = true;
deltax = location.x-heropos.x;
deltay = location.y-heropos.y;
}
return true;
}
void HelloWorld::ccTouchMoved(CCTouch* touch,CCEvent* event){
if(isControl){
CCPoint location = touch->locationInView();
location = CCDirector::sharedDirector()->convertToGL(location);
float x = location.x - deltax;
float y = location.y - deltay;
hero->setPosition(ccp(x,y));
}
}
void HelloWorld::ccTouchEnded(CCTouch* touch,CCEvent* event){
isControl = false;
}
void HelloWorld::ccTouchCancelled(CCTouch* touch,CCEvent* event){
isControl = false;
}
/* 首先在ccTouchBegan函数中判断触摸点是否在主角的图片矩形范围内,若在,将bool型的global变量置为true,并且计算触点的横纵坐标与主角锚点的差值。因为要让主角移动,要修改锚点位置,必须知道锚点位置与触摸位置的偏移量,之后才可以通过这个偏移量设置主角的位置。*/
多点触摸实例:缩放功能的实现
这个功能的实现依然需要在初始化函数中加入setTouchEnabled(true)使得触摸可以被使用,之后便是重写registerWithTouchDispatcher等5个函数:
void HelloWorld::registerWithTouchDispatcher(void){
CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);
}
void HelloWorld::ccTouchesBegan(CCSet* pTouches,CCEvent* pEvent){
if(pTouches->count() >= 2){
CCSetIterator iter = pTouches->begin();
CCPoint mPoint1 = ((CCTouch*)(*iter))->locationInView();
mPoint1 = CCDirector::sharedDirector()->convertToGL(mPoint1);
iter++;
CCPoint mPoint2 = ((CCTouch*)(*iter))->locationInView();
mPoint2 = CCDirector::sharedDirector()->convertToGL(mPoint2);
// 初始时两触点间距离:
distance = sqrt((mPointq.x-mPoint2.x)*(mPoint1.x-mPoint2.x)+(mPoint1.y-mPoint2.y)*(mPoint1.y-mPoint2.y));
deltax = (mPoint1.x+mPoint2.x)/2 - pSprite->getPositionX();
deltay = (mPoint1.y+mPoint2.y)/2 - pSprite->getPositionY();
}
}
void HelloWorld::ccTouchesMoved(CCSet* pTouches,CCEvent* pEvent){
if(pTouches->count()>=2){
CCSetIterator iter = pTouches->begin();
CCPoint mPoint1 = ((CCTouch*)(*iter))->locationInView();
mPoint1 = CCDirector::sharedDirector()->convertToGL(mPoint1);
iter++;
CCPoint mPoint2 =((CCTouch*)(*iter))->locationInView();
mPoint2 = CCDirector::sharedDirector()->convertToGL(mPoint2);
float mdistance = sqrt((mPointq.x-mPoint2.x)*(mPoint1.x-mPoint2.x)+(mPoint1.y-mPoint2.y)*(mPoint1.y-mPoint2.y));
mscale = mistance/distance*mscale; // 缩放比例
distance = mdistance; // 移动过程中两点间新的距离
pSprite->setScale(mscale); // 设置精灵缩放
float x = (mPoint1.x+mPoint2.x)/2-deltax; // 保持两触点中点和精灵锚点距离不变
float y = (mPoint1.y+mPoint2.y)/2-deltax;
pSprite->setPosition(ccp(x,y));
deltax = (mPoint1.x+mPoint2.x)/2 - pSprite->getPositionX();
deltay = (mPoint1.y+mPoint2.y)/2 - pSprite->getPositionY();
}}
void HelloWorld::ccTouchesEnded(CCSet* pTouches,CCEvent* pEvent){
}
void HelloWorld::ccTouchesCancelled(CCSet* pTouches,CCEvent* pEvent){
}
/* 缩放逻辑:在ccTouchesBegan中检测,若触摸点的个数大于两个,那么取前两个点,计算两点间距离;在ccTouchesMoved中检测,若触摸点个数大于两个,那么继续计算这两个点的距离,然后通过距离比得到缩放比例,调用精灵类的setScale设置缩放比例即可。*/
/* 处理位置:在ccTouchBegan中计算两触点中点位置和精灵锚点的差;在ccTouchesMoved中,随着缩放,保持两触点中点与精灵锚点间的差不变即可;ccTouchEnded和ccTouchCancelled中不需要修改。*/