cocos2d-x使用CCScale9Sprite

cocos2d-x在使用CCScale9Sprite的时候需要注意,因为使用到了扩展库所以要

  • 在项目属性中的C/C++的附加包含目录中,添加$(SolutionDir)extensions,设置include路径
  • 在连接器的输入中,添加libExtensions.lib,这个库文件位于cocos2d-x编译结果目录中(Debug.win32)

接下来需要在源码中加入相应的代码,首先是添加头文件及名字空间

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

交叉编译的时候要注意在Android.mk中要加

$(call import-module,extensions) \

方可编译成功

 

 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);

 

按钮事件

 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);  


回调函数

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);  

 

posted @ 2013-07-18 10:08  善小书  阅读(1493)  评论(0编辑  收藏  举报