Cocos2d-x觸摸事件實(shí)例
在玩手機(jī)游戲的時(shí)候,屏幕接收我們的觸摸消息是必不可少的,根據(jù)我們的觸摸事件,去實(shí)現(xiàn)相應(yīng)的功能,這里我們就來(lái)學(xué)習(xí)一下cocos2d-x中的觸摸是怎么實(shí)現(xiàn)的。觸摸分為單點(diǎn)觸摸和多點(diǎn)觸摸,先來(lái)看單點(diǎn)觸摸,就是接收一個(gè)點(diǎn)的觸摸。代碼將實(shí)現(xiàn)過(guò)程清楚的寫(xiě)了下來(lái),仔細(xì)分析代碼吧。
bool HelloWorld::init() { bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); //開(kāi)啟觸摸 this->setTouchEnabled(true); bRet = true; } while (0); return bRet; } //開(kāi)啟觸摸以后必須注冊(cè)觸摸事件,告訴引擎你支持單點(diǎn)觸摸還是多點(diǎn)觸摸 void HelloWorld::registerWithTouchDispatcher() { //addTargetedDelegate注冊(cè)單點(diǎn)觸摸,第一個(gè)參數(shù)代表為哪個(gè)對(duì)象注冊(cè)觸摸事件,第二個(gè)代表優(yōu)先級(jí),數(shù)字越 //小,優(yōu)先級(jí)越高,第三個(gè)參數(shù)代表是否吞噬消息,如果為true這個(gè)節(jié)點(diǎn)接受了消息,處理后,優(yōu)先級(jí)比它小的節(jié)點(diǎn) //就接受不到消息了 CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true); } //ccTouchBegan是必須要實(shí)現(xiàn)的函數(shù),也是在四個(gè)協(xié)議方法中唯一一個(gè)返回值為bool類(lèi)型的函數(shù) //返回值是true的情況下,會(huì)接下來(lái)響應(yīng)ccTouchMoved和ccTouchEnded和ccTouchCancelled函數(shù)。 //CCTouch中封裝了關(guān)于觸摸點(diǎn)的一些屬性,例如坐標(biāo)信息,CCEvent沒(méi)有什么用 bool HelloWorld::ccTouchBegan(CCTouch * pTouch,CCEvent * pEvent) { //獲得opengl坐標(biāo)下的點(diǎn)坐標(biāo) CCPoint point = pTouch->getLocation(); CCSprite * sprite = CCSprite::create("image.png"); this->addChild(sprite); sprite->setPosition(point); return true; } //當(dāng)手指在屏幕上移動(dòng)的時(shí)候不斷調(diào)用的一個(gè)方法 void HelloWorld::ccTouchMoved(CCTouch * touch,CCEvent * pEvent) { //獲得opengl坐標(biāo)下的點(diǎn)坐標(biāo) CCPoint point = touch->getLocation(); CCSprite * sprite = CCSprite::create("image.png"); this->addChild(sprite); sprite->setPosition(point); } //當(dāng)手指抬起來(lái)的時(shí)候會(huì)調(diào)用的方法 void HelloWorld::ccTouchEnded(CCTouch * pTouch,CCEvent * pEvent) { this->removeAllChildrenWithCleanup(true); } //還有一個(gè)ccTouchCancelled函數(shù),在取消觸摸事件的時(shí)候調(diào)用,比如我們?cè)谟|屏的時(shí)候突然打來(lái)了電話(huà)
接下來(lái)用我們剛剛學(xué)到的知識(shí),來(lái)實(shí)現(xiàn)拖拽精靈移動(dòng)的效果。
bool HelloWorld::init() { bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); //實(shí)現(xiàn)拖拽精靈移動(dòng)的效果 CCSprite * sprite = CCSprite::create("image2.png"); sprite->setPosition(ccp(240,180)); this->addChild(sprite,0,0); //開(kāi)啟觸摸 this->setTouchEnabled(true); bRet = true; } while (0); return bRet; } //開(kāi)啟觸摸以后必須注冊(cè)觸摸事件,告訴引擎你支持單點(diǎn)觸摸還是多點(diǎn)觸摸 void HelloWorld::registerWithTouchDispatcher() { //addTargetedDelegate注冊(cè)單點(diǎn)觸摸,第一個(gè)參數(shù)代表為哪個(gè)對(duì)象注冊(cè)觸摸事件,第二個(gè)代表優(yōu)先級(jí),數(shù)字越 //小,優(yōu)先級(jí)越高,第三個(gè)參數(shù)代表是否吞噬消息,如果為true這個(gè)節(jié)點(diǎn)接受了消息,處理后,優(yōu)先級(jí)比它小的節(jié)點(diǎn) //就接受不到消息了 CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true); } bool HelloWorld::ccTouchBegan(CCTouch * touch,CCEvent * pEvent) { CCSprite * sprite = (CCSprite *)this->getChildByTag(0); //獲得手指點(diǎn)擊的點(diǎn)的坐標(biāo) CCPoint point = touch->getLocation(); //獲得精靈所在的區(qū)域,CCRect包括x,y,width,height CCRect rect = sprite->boundingBox(); //判斷手指點(diǎn)擊的點(diǎn)是否點(diǎn)在了精靈上 if(rect.containsPoint(point)) { //返回true則會(huì)接受其他的協(xié)議消息 return true; } return false; } void HelloWorld::ccTouchMoved(CCTouch * touch,CCEvent * pEvent) { /* 這里可以直接將精靈的坐標(biāo)設(shè)置為手指所在點(diǎn)的坐標(biāo)位置,但是這樣的話(huà)會(huì)產(chǎn)生跳躍的效果,視覺(jué)上不好看,這是 因?yàn)榫`是在用自己的錨點(diǎn)占據(jù)我們?cè)O(shè)置的坐標(biāo)位置,而不是我們點(diǎn)擊在精靈身上的那個(gè)點(diǎn)放到我們的手指所在的位置 */ //分別獲得了手指現(xiàn)在的點(diǎn)擊點(diǎn)和手指上次的點(diǎn)擊點(diǎn)位置 CCPoint point = touch->getLocation(); CCPoint pointPre = touch->getPreviousLocation(); //ccpSub將倆個(gè)點(diǎn)相減,獲得一個(gè)移動(dòng)方向的向量 CCPoint direction = ccpSub(point,pointPre); CCSprite * sprite = (CCSprite *)this->getChildByTag(0); CCPoint spritePoint = sprite->getPosition(); //ccpAdd將精靈現(xiàn)在的位置點(diǎn)和移動(dòng)方向的向量相加,獲得精靈將要移動(dòng)到的位置點(diǎn) CCPoint spriteDirection = ccpAdd(spritePoint,direction); sprite->setPosition(spriteDirection); }
接下來(lái)學(xué)習(xí)多點(diǎn)觸摸,多點(diǎn)觸摸和單點(diǎn)觸摸不同的是它的優(yōu)先級(jí)要低于單點(diǎn)觸摸,不論注冊(cè)的時(shí)候里邊傳入的數(shù)字是多少,當(dāng)然還有其它的一些區(qū)別,讓我們看代碼吧。以下是在windows上演示的效果,windows上沒(méi)法實(shí)現(xiàn)多點(diǎn)觸摸。
bool HelloWorld::init() { bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); //實(shí)現(xiàn)拖拽精靈移動(dòng)的效果 CCSprite * sprite = CCSprite::create("image2.png"); sprite->setPosition(ccp(240,180)); this->addChild(sprite,0,0); //開(kāi)啟觸摸 this->setTouchEnabled(true); bRet = true; } while (0); return bRet; } void HelloWorld::registerWithTouchDispatcher() { //注冊(cè)多點(diǎn)觸摸,里邊只有倆個(gè)參數(shù) CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0); } //在多點(diǎn)觸摸中,這四個(gè)協(xié)議函數(shù)在touch后邊都加了es,并且每個(gè)協(xié)議函數(shù)不需要都實(shí)現(xiàn),所有的返回值都是void //CCSet是CCTouch的集合 void HelloWorld::ccTouchesBegan(CCSet * set,CCEvent * pEvent) { //CCSetIterator是一個(gè)迭代器 CCSetIterator iterator; //以下的方法就是從CCSet中獲得對(duì)象的方法 for(iterator = set->begin();iterator != set->end();iterator++) { //將元素強(qiáng)制轉(zhuǎn)化為CCTouch *類(lèi)型 CCTouch * touch = (CCTouch *)(*iterator); CCPoint point = touch->getLocation(); CCSprite * sprite = CCSprite::create("image.png"); sprite->setPosition(point); this->addChild(sprite); } }
接下來(lái)利用上邊的多點(diǎn)觸摸消息實(shí)現(xiàn)精靈的放大和縮放效果,大家看到的相冊(cè)圖片放大和縮小的效果也是這么實(shí)現(xiàn)的,但是windows不支持多點(diǎn),所以這里只是說(shuō)明原理。
查看源代碼打印幫助
bool HelloWorld::init() { bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); //實(shí)現(xiàn)拖拽精靈移動(dòng)的效果 CCSprite * sprite = CCSprite::create("image2.png"); sprite->setPosition(ccp(240,180)); this->addChild(sprite,0,0); //開(kāi)啟觸摸 this->setTouchEnabled(true); bRet = true; } while (0); return bRet; } void HelloWorld::registerWithTouchDispatcher() { //注冊(cè)多點(diǎn)觸摸,里邊只有倆個(gè)參數(shù) CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0); } void HelloWorld::ccTouchesBegan(CCSet * set,CCEvent * pEvent) { CCSetIterator iterator = set->begin(); //獲得第一個(gè)觸摸點(diǎn) CCTouch * touch0 = (CCTouch *)(*iterator); iterator++; //程序執(zhí)行到這里會(huì)死掉,因?yàn)閣indows只支持單點(diǎn)觸摸,不支持多點(diǎn),所以這里是不會(huì)獲得第二個(gè)點(diǎn)的 CCTouch * touch1 = (CCTouch *)(*iterator); length = ccpDistance(touch0->getLocation(),touch1->getLocation()); } void HelloWorld::ccTouchesMoved(CCSet * set,CCEvent * pEvent) { CCSetIterator iterator = set->begin(); CCTouch * touch0 = (CCTouch *)(*iterator); iterator++; CCTouch * touch1 = (CCTouch *)(*iterator); float length2 = ccpDistance(touch0->getLocation(),touch1->getLocation()); float times = length2/length; CCSprite * sprite = (CCSprite *)this->getChildByTag(0); sprite->setScale(times); }
上一篇:Cocos2d-x UI開(kāi)發(fā)之CCControlButton控件類(lèi)實(shí)例
欄 目:C語(yǔ)言
下一篇:Cocos2d-x學(xué)習(xí)筆記之CCScene、CCLayer、CCSprite的默認(rèn)坐標(biāo)和默認(rèn)錨點(diǎn)實(shí)驗(yàn)
本文標(biāo)題:Cocos2d-x觸摸事件實(shí)例
本文地址:http://mengdiqiu.com.cn/a1/Cyuyan/3393.html
您可能感興趣的文章
- 01-10模擬鼠標(biāo)事件的實(shí)現(xiàn)思路及代碼
- 01-10Cocos2d-x學(xué)習(xí)入門(mén)之HelloWorld程序
- 01-10Cocos2d-x學(xué)習(xí)筆記之Hello World!
- 01-10Cocos2d-x UI開(kāi)發(fā)之場(chǎng)景切換代碼實(shí)例
- 01-10Cocos2d-x學(xué)習(xí)筆記之Hello World源碼分析
- 01-10Cocos2d-x UI開(kāi)發(fā)之CCControlSlider控件類(lèi)使用實(shí)例
- 01-10Cocos2d-x學(xué)習(xí)筆記之CCLayerColor層的使用實(shí)例
- 01-10cocos2d-x學(xué)習(xí)筆記之CCLayer、CCLayerColor、CCLayerGradient、CCLayerMu
- 01-10Cocos2d-x UI開(kāi)發(fā)之CCControlSwitch控件類(lèi)使用實(shí)例
- 01-10Cocos2d-x學(xué)習(xí)筆記之CCScene、CCLayer、CCSprite的默認(rèn)坐標(biāo)和默認(rèn)錨點(diǎn)實(shí)


閱讀排行
- 1C語(yǔ)言 while語(yǔ)句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹(shù)的示例代碼(圣誕
- 3利用C語(yǔ)言實(shí)現(xiàn)“百馬百擔(dān)”問(wèn)題方法
- 4C語(yǔ)言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語(yǔ)言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語(yǔ)言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語(yǔ)言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 04-02c語(yǔ)言函數(shù)調(diào)用后清空內(nèi)存 c語(yǔ)言調(diào)用
- 04-02func函數(shù)+在C語(yǔ)言 func函數(shù)在c語(yǔ)言中
- 04-02c語(yǔ)言的正則匹配函數(shù) c語(yǔ)言正則表達(dá)
- 04-02c語(yǔ)言用函數(shù)寫(xiě)分段 用c語(yǔ)言表示分段
- 04-02c語(yǔ)言中對(duì)數(shù)函數(shù)的表達(dá)式 c語(yǔ)言中對(duì)
- 04-02c語(yǔ)言編寫(xiě)函數(shù)冒泡排序 c語(yǔ)言冒泡排
- 04-02c語(yǔ)言沒(méi)有round函數(shù) round c語(yǔ)言
- 04-02c語(yǔ)言分段函數(shù)怎么求 用c語(yǔ)言求分段
- 04-02C語(yǔ)言中怎么打出三角函數(shù) c語(yǔ)言中怎
- 04-02c語(yǔ)言調(diào)用函數(shù)求fibo C語(yǔ)言調(diào)用函數(shù)求
隨機(jī)閱讀
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-11Mac OSX 打開(kāi)原生自帶讀寫(xiě)NTFS功能(圖文
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-10使用C語(yǔ)言求解撲克牌的順子及n個(gè)骰子
- 01-11ajax實(shí)現(xiàn)頁(yè)面的局部加載
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 01-10C#中split用法實(shí)例總結(jié)
- 01-10SublimeText編譯C開(kāi)發(fā)環(huán)境設(shè)置