10 cocos2d-x 按钮 CCControlButton
2013-12-20 16:33
615 查看
#ifndef __HELLOWORLD_SCENE_H__ #define __HELLOWORLD_SCENE_H__ #include "cocos2d.h" #include "cocos-ext.h" using namespace cocos2d::extension; class HelloWorld : public cocos2d::CCLayer { public: // Method 'init' in cocos2d-x returns bool, instead of 'id' in cocos2d-iphone (an object pointer) virtual bool init(); // there's no 'id' in cpp, so we recommend to return the class instance pointer static cocos2d::CCScene* scene(); // a selector callback void menuCloseCallback(CCObject* pSender); //按下事件的回调 void touchDownAction(CCObject *sender,CCControlEvent * event ); //按钮在七内部抬起事件的回调 void touchUpInsideAction(CCObject *sender,CCControlEvent * event ); //按钮在七外部抬起的事件的回调 void touchUpOutSideAction(CCObject *sender,CCControlEvent * event); // preprocessor macro for "static create()" constructor ( node() deprecated ) CREATE_FUNC(HelloWorld); }; #endif // __HELLOWORLD_SCENE_H__
#include "HelloWorldScene.h" #include "SimpleAudioEngine.h" #include "cocos-ext.h" #include "CCControlButton.h" using namespace cocos2d; using namespace CocosDenshion; using namespace cocos2d::extension; CCScene* HelloWorld::scene() { // 'scene' is an autorelease object CCScene *scene = CCScene::create(); // 'layer' is an autorelease object HelloWorld *layer = HelloWorld::create(); // add layer as a child to scene scene->addChild(layer); // return the scene return scene; } // on "init" you need to initialize your instance bool HelloWorld::init() { ////////////////////////////// // 1. super init first if ( !CCLayer::init() ) { return false; } //CCControlButton 按钮 CCLabelTTF *label=CCLabelTTF::create("按钮", "MarkerFelt", 20); CCScale9Sprite *sprite=CCScale9Sprite::create("Icon.png"); CCControlButton *btn=CCControlButton::create(label, sprite); btn->setPosition(ccp(240, 170)); //按钮被选中背景图片响应的状态 btn->setBackgroundSpriteForState(CCScale9Sprite::create("123.png"), CCControlStateHighlighted); //按钮被选中后文字颜色响应的状态 btn->setTitleColorForState(ccc3(0, 0, 255), CCControlStateHighlighted); //按钮被选中后文字响应的状态 btn->setTitleForState(CCString::create("选中"), CCControlStateHighlighted); this->addChild(btn); //按钮按下事件回调 btn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchDownAction), CCControlEventTouchDown); //按钮在七内部抬起事件的回调 btn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchUpInsideAction), CCControlEventTouchUpInside); //按钮在七外部抬起的事件的回调 btn->addTargetWithActionForControlEvents(this, cccontrol_selector(HelloWorld::touchUpOutSideAction), CCControlEventTouchUpOutside); //用于显示按钮的状态 CCLabelTTF *label2=CCLabelTTF::create("", "MarkerFelt", 20); label2->setPosition(ccp(240, 220)); this->addChild(label2,0,90); return true; } //按下事件的回调 void HelloWorld::touchDownAction(CCObject *sender,CCControlEvent * event ) { CCLabelTTF *label=(CCLabelTTF *)this->getChildByTag(90); label->setString(CCString::createWithFormat("按下")->getCString()); } //按钮在七内部抬起事件的回调 void HelloWorld::touchUpInsideAction(CCObject *sender,CCControlEvent * event ) { CCLabelTTF *label=(CCLabelTTF *)this->getChildByTag(90); label->setString(CCString::createWithFormat("内部抬起")->getCString()); } //按钮在七外部抬起的事件的回调 void HelloWorld::touchUpOutSideAction(CCObject *sender,CCControlEvent * event) { CCLabelTTF *label=(CCLabelTTF *)this->getChildByTag(90); label->setString(CCString::createWithFormat("外部抬起")->getCString()); }
相关文章推荐
- 【COCOS2DX-ANDROID-游戏开发之二五】 调试 C++代码
- 在cocos2dx 中添加自定义 shaders --- 改变 HSL 色调,饱和度, 亮度
- Cocos2d-x Application Wizard for Visual Studio User Guide
- 9 cocos2d-x CCControlSlider (滑动条)
- 8.cocos2dx 开关 CCControlSwitch
- cocos2d-x 有关CCEditBox设置除输入框以外的可点击触发区域
- 7 cocos2d-x CCEditBox 编辑框
- (转)cocos2d-x 数学函数、常用宏粗整理 - by Glede
- 6 cocos2dx 菜单项
- cocos2d-x实现透视朦胧光照效果
- cocos2dx之CCAccelerometerDelegate
- 4.cocos2d-x CCNode
- Cocos2d-x教程 CocoStudio篇 (2) UIButton控件
- 2.cocos2d 触摸的开启
- 1.cocos2d CCLayer 的创建
- Cocos2dx游戏开发系列笔记13:一个横版拳击游戏Demo完结篇
- XCode5下安装cocos2d模板及不能成功安装的解决办法
- Cocos2dx在helloCpp加入Cocosdenshion时出现了 Undefined symbols for architecture i386
- 【cocos2D-x入门之环境配置 WIN7 + VS2010 + cocos2D-x-2.2.1】
- fix the elipse compiling err when using memcpy in cocos2d-x