在玩手機游戲的時候,屏幕接收我們的觸摸消息是必不可少的,根據我們的觸摸事件,去實現相應的功能,這里我們就來學習一下cocos2d-x中的觸摸是怎么實現的。觸摸分為單點觸摸和多點觸摸,先來看單點觸摸,就是接收一個點的觸摸。代碼將實現過程清楚的寫了下來,仔細分析代碼吧。
bool HelloWorld::init(){ bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); //開啟觸摸 this->setTouchEnabled(true); bRet = true; } while (0); return bRet;}//開啟觸摸以后必須注冊觸摸事件,告訴引擎你支持單點觸摸還是多點觸摸void HelloWorld::registerWithTouchDispatcher(){ //addTargetedDelegate注冊單點觸摸,第一個參數代表為哪個對象注冊觸摸事件,第二個代表優先級,數字越 //小,優先級越高,第三個參數代表是否吞噬消息,如果為true這個節點接受了消息,處理后,優先級比它小的節點 //就接受不到消息了 CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);}//ccTouchBegan是必須要實現的函數,也是在四個協議方法中唯一一個返回值為bool類型的函數//返回值是true的情況下,會接下來響應ccTouchMoved和ccTouchEnded和ccTouchCancelled函數。//CCTouch中封裝了關于觸摸點的一些屬性,例如坐標信息,CCEvent沒有什么用bool HelloWorld::ccTouchBegan(CCTouch * pTouch,CCEvent * pEvent){ //獲得opengl坐標下的點坐標 CCPoint point = pTouch->getLocation(); CCSprite * sprite = CCSprite::create("image.png"); this->addChild(sprite); sprite->setPosition(point); return true;}//當手指在屏幕上移動的時候不斷調用的一個方法void HelloWorld::ccTouchMoved(CCTouch * touch,CCEvent * pEvent){ //獲得opengl坐標下的點坐標 CCPoint point = touch->getLocation(); CCSprite * sprite = CCSprite::create("image.png"); this->addChild(sprite); sprite->setPosition(point);}//當手指抬起來的時候會調用的方法void HelloWorld::ccTouchEnded(CCTouch * pTouch,CCEvent * pEvent){ this->removeAllChildrenWithCleanup(true);}//還有一個ccTouchCancelled函數,在取消觸摸事件的時候調用,比如我們在觸屏的時候突然打來了電話
接下來用我們剛剛學到的知識,來實現拖拽精靈移動的效果。
bool HelloWorld::init(){ bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); //實現拖拽精靈移動的效果 CCSprite * sprite = CCSprite::create("image2.png"); sprite->setPosition(ccp(240,180)); this->addChild(sprite,0,0); //開啟觸摸 this->setTouchEnabled(true); bRet = true; } while (0); return bRet;}//開啟觸摸以后必須注冊觸摸事件,告訴引擎你支持單點觸摸還是多點觸摸void HelloWorld::registerWithTouchDispatcher(){ //addTargetedDelegate注冊單點觸摸,第一個參數代表為哪個對象注冊觸摸事件,第二個代表優先級,數字越 //小,優先級越高,第三個參數代表是否吞噬消息,如果為true這個節點接受了消息,處理后,優先級比它小的節點 //就接受不到消息了 CCDirector::sharedDirector()->getTouchDispatcher()->addTargetedDelegate(this,0,true);}bool HelloWorld::ccTouchBegan(CCTouch * touch,CCEvent * pEvent){ CCSprite * sprite = (CCSprite *)this->getChildByTag(0); //獲得手指點擊的點的坐標 CCPoint point = touch->getLocation(); //獲得精靈所在的區域,CCRect包括x,y,width,height CCRect rect = sprite->boundingBox(); //判斷手指點擊的點是否點在了精靈上 if(rect.containsPoint(point)) { //返回true則會接受其他的協議消息 return true; } return false;}void HelloWorld::ccTouchMoved(CCTouch * touch,CCEvent * pEvent){ /* 這里可以直接將精靈的坐標設置為手指所在點的坐標位置,但是這樣的話會產生跳躍的效果,視覺上不好看,這是 因為精靈是在用自己的錨點占據我們設置的坐標位置,而不是我們點擊在精靈身上的那個點放到我們的手指所在的位置 */ //分別獲得了手指現在的點擊點和手指上次的點擊點位置 CCPoint point = touch->getLocation(); CCPoint pointPre = touch->getPreviousLocation(); //ccpSub將倆個點相減,獲得一個移動方向的向量 CCPoint direction = ccpSub(point,pointPre); CCSprite * sprite = (CCSprite *)this->getChildByTag(0); CCPoint spritePoint = sprite->getPosition(); //ccpAdd將精靈現在的位置點和移動方向的向量相加,獲得精靈將要移動到的位置點 CCPoint spriteDirection = ccpAdd(spritePoint,direction); sprite->setPosition(spriteDirection);}
接下來學習多點觸摸,多點觸摸和單點觸摸不同的是它的優先級要低于單點觸摸,不論注冊的時候里邊傳入的數字是多少,當然還有其它的一些區別,讓我們看代碼吧。以下是在windows上演示的效果,windows上沒法實現多點觸摸。
bool HelloWorld::init(){ bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); //實現拖拽精靈移動的效果 CCSprite * sprite = CCSprite::create("image2.png"); sprite->setPosition(ccp(240,180)); this->addChild(sprite,0,0); //開啟觸摸 this->setTouchEnabled(true); bRet = true; } while (0); return bRet;}void HelloWorld::registerWithTouchDispatcher(){ //注冊多點觸摸,里邊只有倆個參數 CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);}//在多點觸摸中,這四個協議函數在touch后邊都加了es,并且每個協議函數不需要都實現,所有的返回值都是void//CCSet是CCTouch的集合void HelloWorld::ccTouchesBegan(CCSet * set,CCEvent * pEvent){ //CCSetIterator是一個迭代器 CCSetIterator iterator; //以下的方法就是從CCSet中獲得對象的方法 for(iterator = set->begin();iterator != set->end();iterator++) { //將元素強制轉化為CCTouch *類型 CCTouch * touch = (CCTouch *)(*iterator); CCPoint point = touch->getLocation(); CCSprite * sprite = CCSprite::create("image.png"); sprite->setPosition(point); this->addChild(sprite); }}
接下來利用上邊的多點觸摸消息實現精靈的放大和縮放效果,大家看到的相冊圖片放大和縮小的效果也是這么實現的,但是windows不支持多點,所以這里只是說明原理。
查看源代碼打印幫助
bool HelloWorld::init(){ bool bRet = false; do { CC_BREAK_IF(! CCLayer::init()); //實現拖拽精靈移動的效果 CCSprite * sprite = CCSprite::create("image2.png"); sprite->setPosition(ccp(240,180)); this->addChild(sprite,0,0); //開啟觸摸 this->setTouchEnabled(true); bRet = true; } while (0); return bRet;}void HelloWorld::registerWithTouchDispatcher(){ //注冊多點觸摸,里邊只有倆個參數 CCDirector::sharedDirector()->getTouchDispatcher()->addStandardDelegate(this,0);}void HelloWorld::ccTouchesBegan(CCSet * set,CCEvent * pEvent){ CCSetIterator iterator = set->begin(); //獲得第一個觸摸點 CCTouch * touch0 = (CCTouch *)(*iterator); iterator++; //程序執行到這里會死掉,因為windows只支持單點觸摸,不支持多點,所以這里是不會獲得第二個點的 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);}
新聞熱點
疑難解答
圖片精選