cocos2d-x使用CCScale9Sprite

cocos2d-x在使用CCScale9Sprite的時候須要注意,由於使用到了擴展庫因此要ide

  • 在項目屬性中的C/C++的附加包含目錄中,添加$(SolutionDir)extensions,設置include路徑
  • 在鏈接器的輸入中,添加libExtensions.lib,這個庫文件位於cocos2d-x編譯結果目錄中(Debug.win32)

接下來須要在源碼中加入相應的代碼,首先是添加頭文件及名字空間函數

1 #include "cocos-ext.h" 
2   
3  USING_NS_CC_EXT;  

交叉編譯的時候要注意在Android.mk中要加this

$(call import-module,extensions) \spa

方可編譯成功code

 

 1         /////////////////////////////
 2         //添加跳轉按鈕
 3         //正常態下按鈕圖片
 4         CCScale9Sprite *butNormal = CCScale9Sprite::create("up2.png");
 5         //選中後按鈕圖片
 6         CCScale9Sprite *butSelected = CCScale9Sprite::create("up1.png");
 7         //按鈕標題
 8         //CCLabelTTF *title = CCLabelTTF::create("Button","Arial",20);
 9         //按鈕能夠根據標題自動調整大小,固然也能夠設定
10         CCControlButton *pButton = CCControlButton::create(butNormal);
11         pButton->setPreferredSize(CCSizeMake(48,48));
12         //設置選中後圖片
13         pButton->setBackgroundSpriteForState(butSelected,CCControlStateSelected);
14         pButton->setPosition(ccp(size.width / 2, size.height/2));
15         this->addChild(pButton);

 

按鈕事件orm

 1     /* 當鼠標處於按下並曾經點中按鈕時,則觸發一次 */  
 2         controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDownAction), CCControlEventTouchDown);  
 3       
 4         /* 當鼠標處於按下並曾經點中按鈕的狀態下,鼠標進入按鈕範圍,則觸發一次 */  
 5         controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDragEnter), CCControlEventTouchDragEnter);  
 6       
 7         /* 當鼠標處於按下並曾經點中按鈕的狀態下,鼠標離開按鈕範圍,則觸發一次 */  
 8         controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDragExit), CCControlEventTouchDragExit);  
 9       
10         /* 當鼠標處於按下並曾經點中按鈕的狀態下,鼠標進入按鈕範圍,則觸發,只要達到條件,就不斷觸發 */  
11         controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDragInside), CCControlEventTouchDragInside);  
12       
13         /* 當鼠標處於按下並曾經點中按鈕的狀態下,鼠標離開按鈕範圍,則觸發,只要達到條件,就不斷觸發 */  
14         controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDragOutside), CCControlEventTouchDragOutside);  
15       
16         /* 當鼠標處於按下並曾經點中按鈕的狀態下,鼠標鬆開且在按鈕範圍內,則觸發一次 */  
17         controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchUpInside), CCControlEventTouchUpInside);  
18       
19         /* 當鼠標處於按下並曾經點中按鈕的狀態下,鼠標鬆開且在按鈕範圍外,則觸發一次 */  
20         controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchUpOutside), CCControlEventTouchUpOutside);  
21       
22         /* 暫時沒有發現能用鼠標觸發這個事件的操做,看了註釋,應該是由其它事件中斷按鈕事件而觸發的 */  
23         controlBtn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchCancel), CCControlEventTouchCancel);  


回調函數blog

1     void touchDownAction(CCObject* pSender, CCControlEvent event);  
2        void touchDragEnter(CCObject* pSender, CCControlEvent event);  
3        void touchDragExit(CCObject* pSender, CCControlEvent event);  
4        void touchDragInside(CCObject* pSender, CCControlEvent event);  
5        void touchDragOutside(CCObject* pSender, CCControlEvent event);  
6        void touchUpInside(CCObject* pSender, CCControlEvent event);  
7        void touchUpOutside(CCObject* pSender, CCControlEvent event);  
8        void touchCancel(CCObject* pSender, CCControlEvent event);  
相關文章
相關標籤/搜索