cocos2d-x基本知识点:事件处理机制之触屏事件2

单点触摸实例:主角随手指移动

首先在布景层的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中不需要修改。*/







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值