Cocos2d-x實例:單點觸摸事件

  1.     addChild(boxC,30, kBoxC_Tag);                                                                                         ⑥  php

  2.    

  3.     returntrue;  html

  4. }  函數

 

 

咱們在HelloWorld::init()函數中初始化了場景中的背景和三個方塊精靈。代碼第①~④行是建立並添加背景,圖8-3所示的背景是由一個128x128紋理圖片(BackgroundTile.png)反覆貼圖上,這樣能夠減小內存消耗,在第①行代碼中建立背景精靈對象,注意背景的大小仍然是整個屏幕。第②行代碼是設置貼圖的紋理的參數,Texture2D::TexParams類型是一個結構體。第③行代碼是將參數設置到背景精靈的紋理上。第④行代碼是添加背景精靈到當前層。測試

代碼第⑤~⑥行是建立了三個方塊精靈,在添加它到當前層的時候咱們使用三個參數的addChild(Node* child,int localZOrder,int tag)函數,這樣能夠經過localZOrder參數指定精靈的顯示順序。網站

 

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片this

 

  1. HelloWorldScene.ccp中的HelloWorld::onEnter()代碼以下:  spa

  2. void HelloWorld::onEnter()  .net

  3. {  code

  4.     Layer::onEnter();  orm

  5.     log("HelloWorldonEnter");  

  6.    

  7.     autolistener = EventListenerTouchOneByOne::create();                                                             ①  

  8.      

  9.     listener->setSwallowTouches(true);                                                                                      ②  

  10.     listener->onTouchBeganCC_CALLBACK_2(HelloWorld::touchBegan, this);                         ③  

  11.     listener->onTouchMoved=  CC_CALLBACK_2(HelloWorld::touchMoved,this);                     ④  

  12.     listener->onTouchEnded=  CC_CALLBACK_2(HelloWorld::touchEnded,this);                     ⑤  

  13.    

  14.     //添加監聽器  

  15.     EventDispatcher*eventDispatcher = Director::getInstance()->getEventDispatcher();              ⑥  

  16.     eventDispatcher->addEventListenerWithSceneGraphPriority(listener,  

  17.                                                                             getChildByTag(kBoxA_Tag));                                  ⑦  

  18.     eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),  

  19.                                                                             getChildByTag(kBoxB_Tag));                                  ⑧  

  20.     eventDispatcher->addEventListenerWithSceneGraphPriority(listener->clone(),  

  21.                                                                             getChildByTag(kBoxC_Tag));                                 ⑨  

  22.    

  23. }  

 

上述代碼第①行是建立一個單點觸摸事件監聽器對象。第②行代碼是設置是否吞沒事件,若是設置爲true,那麼在onTouchBegan函數返回 true 時吞沒事件,事件不會傳遞給下一個Node對象。第③行代碼是設置監聽器的onTouchBegan屬性回調函數。第④行代碼是設置監聽器的onTouchMoved屬性回調函數。第⑤行代碼是設置監聽器的onTouchEnded屬性回調函數。

代碼第⑥~⑨行是添加監聽器,其中第⑦行使用精靈顯示優先級添加事件監聽器,其中參數getChildByTag(kBoxA_Tag)是經過精靈標籤Tag實現得到精靈對象。第⑧行和第⑨行代碼是爲另外兩精靈添加事件監聽器,其中listener->clone()得到listener對象,使用clone()函數是由於每個事件監聽器只能被添加一次,addEventListenerWithSceneGraphPriority和addEventListenerWithFixedPriority會在添加事件監聽器時設置一個註冊標識,一旦設置了註冊標識,該監聽器就不能再用於註冊其它事件監聽了,所以咱們須要使用listener->clone()克隆一個新的監聽器對象,把這個新的監聽器對象用於註冊。

HelloWorldScene.ccp中的觸摸事件回調函數代碼以下:

 

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片

 

  1. bool HelloWorld::touchBegan(Touch*touch, Event* event)                                                            ①  

  2. {  

  3.     //獲取事件所綁定的 target  

  4.     autotarget = static_cast<Sprite*>(event->getCurrentTarget());                                                    ②  

  5.     PointlocationInNode = target->convertToNodeSpace(touch->getLocation());                         ③  

  6.     Sizes = target->getContentSize();                                                                                          ④  

  7.     Rectrect = Rect(0, 0, s.width, s.height);                                                                                         ⑤  

  8.    

  9.     //點擊範圍判斷檢測  

  10.     if(rect.containsPoint(locationInNode))                                                                                         ⑥  

  11.     {  

  12.          log("spritex = %f, y = %f ", locationInNode.x, locationInNode.y);  

  13.          log("spritetag = %d", target->getTag());  

  14.          target->runAction(ScaleBy::create(0.06f,1.06f));                                                             ⑦  

  15.          returntrue;                                                                                                                                     ⑧  

  16.     }  

  17.     returnfalse;  

  18. }  

  19.    

  20. void HelloWorld::touchMoved(Touch*touch, Event *event)                                                           ⑨  

  21. {  

  22.     log("onTouchMoved");  

  23.     autotarget = static_cast<Sprite*>(event->getCurrentTarget());  

  24.     target->setPosition(target->getPosition()+ touch->getDelta());                                                   ⑩  

  25. }  

  26.    

  27. void HelloWorld::touchEnded(Touch*touch, Event *event)                                                           ⑪  

  28. {  

  29.     log("onTouchEnded");  

  30.     autotarget = static_cast<Sprite*>(event->getCurrentTarget());  

  31.     log("spriteonTouchesEnded.. ");  

  32.    

  33.     PointlocationInNode = target->convertToNodeSpace(touch->getLocation());  

  34.     Sizes = target->getContentSize();  

  35.     Rectrect = Rect(0, 0, s.width, s.height);  

  36.     //點擊範圍判斷檢測  

  37.     if(rect.containsPoint(locationInNode))  

  38.     {  

  39.          log("spritex = %f, y = %f ", locationInNode.x, locationInNode.y);  

  40.          log("spritetag = %d", target->getTag());  

  41.          target->runAction(ScaleTo::create(0.06f,1.0f));  

  42.     }  

  43. }  

 

 

上代碼第①行是定義回調函數touchBegan。第②行代碼是獲取事件所綁定的精靈對象,其中event->getCurrentTarget()語句返回值是Node對象,static_cast<Sprite*>是強制類型轉換爲Sprite對象。第③行代碼是獲取當前觸摸點相對於target對象的本地座標。第④行代碼是得到target對象的尺寸。第⑤行代碼是經過target對象的尺寸建立Rect變量。第⑥行代碼rect.containsPoint(locationInNode)是判斷是否觸摸點在target對象範圍。第⑦行代碼是放大target對象。第⑧行代碼返回true,表示能夠回調第⑨行touchMoved函數和第⑪行touchEnded函數。第⑩行代碼是移動target對象的位置。

HelloWorldScene.ccp中的HelloWorld::onExit()代碼以下:

 

[html] view plaincopy在CODE上查看代碼片派生到個人代碼片

 

  1. void HelloWorld::onExit()  

  2. {  

  3.     Layer::onExit();  

  4.     log("HelloWorldonExit");  

  5.     Director::getInstance()->getEventDispatcher()->removeAllEventListeners();  

  6. }  

 

 

上述HelloWorld::onExit()函數是退出層時候回調,咱們在這個函數中註銷全部的監聽事件。

提示 多點觸摸事件是與具體的平臺有關係的,在Win32平臺下咱們沒法測試多點觸摸。事實上多點觸摸和單點觸摸開發流程基本類似,這裏咱們就再也不贅述了。

 

更多內容請關注Cocos2d-x系列圖書《Cocos2d-x實戰(卷Ⅰ):C++開發》

本書交流討論網站:http://www.cocoagame.net

歡迎加入cocos2d-x技術討論羣:25776038六、327403678

相關文章
相關標籤/搜索