cocos2d-x2.1 GUI控件的使用
2014-04-13 06:03
267 查看
2013-03-06 17:38
在使用extensions时都需要额外指定include、lib,例如笔者的VS11环境,右键项目 属性-->配置属性-->VC++目录在包含目录中添加F:\cocos2d-2.1beta3-x-2.1.1\extensions;以及在链接器-->命令行添加libextensions.lib或者使用代码添加
[cpp] view
plaincopy
#pragma comment(lib, "libextensions.lib")
进入程序中需要将使用到的头文件写入,可以直接写#include “cocos-ext.h”(它包含了extensions的所有头文件引用),然后添加命名空间声明:USING_NS_CC_EXT;
对于各种GUI控件可以在Samples的TestCpp的ExtensionsTest\ControlExtensionTest查看学习
下面这个代码参考了CCControlSwitchTest
[cpp] view
plaincopy
#pragma once
#include "cocos2d.h"
#include "cocos-ext.h"
#pragma comment(lib, "libextensions.lib")
USING_NS_CC_EXT;
class HelpLayer : public cocos2d::CCLayer
{
public:
HelpLayer(void);
~HelpLayer(void);
bool init();
CREATE_FUNC(HelpLayer);
cocos2d::CCLabelTTF *m_valueLabel;
void valueChanged(cocos2d::CCObject *sender, CCControlEvent controlEvent);
};
[cpp] view
plaincopy
#include "HelpLayer.h"
using namespace cocos2d;
HelpLayer::HelpLayer(void)
{
m_valueLabel = NULL;
}
HelpLayer::~HelpLayer(void)
{
CC_SAFE_RELEASE(m_valueLabel);
}
bool HelpLayer::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(!CCLayer::init());
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
// switch controler
CCControlSwitch *switchControl = CCControlSwitch::create
(
CCSprite::create("extensions/switch-mask.png"),
CCSprite::create("extensions/switch-on.png"),
CCSprite::create("extensions/switch-off.png"),
CCSprite::create("extensions/switch-thumb.png"),
CCLabelTTF::create("on", "Arial-BoldMT", 16),
CCLabelTTF::create("off", "Arial-BoldMT", 16)
);
switchControl->setOn(true);
switchControl->setPosition(ccp(winSize.width / 2 + 50, winSize.height / 2));
this->addChild(switchControl);
switchControl->addTargetWithActionForControlEvents(this, cccontrol_selector(HelpLayer::valueChanged), CCControlEventValueChanged);
// button
CCScale9Sprite *background = CCScale9Sprite::create("extensions/buttonBackground.png");
background->setContentSize(CCSizeMake(80, 50));
background->setPosition(ccp(winSize.width / 2 - 100, winSize.height / 2));
this->addChild(background);
m_valueLabel = CCLabelTTF::create("ON", "Marker Felt", 30);
m_valueLabel->retain();
m_valueLabel->setPosition(background->getPosition());
this->addChild(m_valueLabel);
bRet = true;
} while (0);
return bRet;
}
void HelpLayer::valueChanged(cocos2d::CCObject *sender, CCControlEvent controlEvent)
{
CCControlSwitch *pSwitch = (CCControlSwitch*)sender;
if (pSwitch->isOn())
{
m_valueLabel->setString("ON");
}
else
{
m_valueLabel->setString("OFF");
}
}
程序运行如下:
在使用extensions时都需要额外指定include、lib,例如笔者的VS11环境,右键项目 属性-->配置属性-->VC++目录在包含目录中添加F:\cocos2d-2.1beta3-x-2.1.1\extensions;以及在链接器-->命令行添加libextensions.lib或者使用代码添加
[cpp] view
plaincopy
#pragma comment(lib, "libextensions.lib")
进入程序中需要将使用到的头文件写入,可以直接写#include “cocos-ext.h”(它包含了extensions的所有头文件引用),然后添加命名空间声明:USING_NS_CC_EXT;
对于各种GUI控件可以在Samples的TestCpp的ExtensionsTest\ControlExtensionTest查看学习
下面这个代码参考了CCControlSwitchTest
[cpp] view
plaincopy
#pragma once
#include "cocos2d.h"
#include "cocos-ext.h"
#pragma comment(lib, "libextensions.lib")
USING_NS_CC_EXT;
class HelpLayer : public cocos2d::CCLayer
{
public:
HelpLayer(void);
~HelpLayer(void);
bool init();
CREATE_FUNC(HelpLayer);
cocos2d::CCLabelTTF *m_valueLabel;
void valueChanged(cocos2d::CCObject *sender, CCControlEvent controlEvent);
};
[cpp] view
plaincopy
#include "HelpLayer.h"
using namespace cocos2d;
HelpLayer::HelpLayer(void)
{
m_valueLabel = NULL;
}
HelpLayer::~HelpLayer(void)
{
CC_SAFE_RELEASE(m_valueLabel);
}
bool HelpLayer::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(!CCLayer::init());
CCSize winSize = CCDirector::sharedDirector()->getWinSize();
// switch controler
CCControlSwitch *switchControl = CCControlSwitch::create
(
CCSprite::create("extensions/switch-mask.png"),
CCSprite::create("extensions/switch-on.png"),
CCSprite::create("extensions/switch-off.png"),
CCSprite::create("extensions/switch-thumb.png"),
CCLabelTTF::create("on", "Arial-BoldMT", 16),
CCLabelTTF::create("off", "Arial-BoldMT", 16)
);
switchControl->setOn(true);
switchControl->setPosition(ccp(winSize.width / 2 + 50, winSize.height / 2));
this->addChild(switchControl);
switchControl->addTargetWithActionForControlEvents(this, cccontrol_selector(HelpLayer::valueChanged), CCControlEventValueChanged);
// button
CCScale9Sprite *background = CCScale9Sprite::create("extensions/buttonBackground.png");
background->setContentSize(CCSizeMake(80, 50));
background->setPosition(ccp(winSize.width / 2 - 100, winSize.height / 2));
this->addChild(background);
m_valueLabel = CCLabelTTF::create("ON", "Marker Felt", 30);
m_valueLabel->retain();
m_valueLabel->setPosition(background->getPosition());
this->addChild(m_valueLabel);
bRet = true;
} while (0);
return bRet;
}
void HelpLayer::valueChanged(cocos2d::CCObject *sender, CCControlEvent controlEvent)
{
CCControlSwitch *pSwitch = (CCControlSwitch*)sender;
if (pSwitch->isOn())
{
m_valueLabel->setString("ON");
}
else
{
m_valueLabel->setString("OFF");
}
}
程序运行如下:
相关文章推荐
- cocos2d控件(1)-使用URL创建的Sprite
- unity3d插件Daikon Forge GUI 中文教程3-基础控件Button和Sprite的使用
- cocos2d-x 类似进度条的控件使用
- Cocos2d-x UI开发之CCControlPotentiometer控件类使用实例
- xcode7中使用cocos2d-x3.8的webview控件
- cocos2d-x-2.0新增加了几个UI控件,这里我介绍下常用的这几个UI控件(CCControlSlider、CCControlSwitch、CCControlColourPicker)使用方法
- unity3d插件Daikon Forge GUI 中文教程-3-基础控件Button和Sprite的使用
- unity3d插件Daikon Forge GUI 中文教程4-高级控件Checkbox和dropdownlis的使用
- unity3d插件Daikon Forge GUI 中文教程-6-高级控件richtextlabel的使用
- Unity3D GUI学习之GUILayout控件及使用
- cocos2d-x-2.0新增加了几个UI控件,这里我介绍下常用的这几个UI控件(CCControlSlider、CCControlSwitch、CCControlColourPicker)使用方法
- cocos2d-x GUI控件的使用(cocos2d-x2.1)
- Cocos2d-x UI开发之CCControlSlider控件类使用实例
- Cocos2d-x中控件CCControlStepper的使用
- unity3d插件Daikon Forge GUI 中文教程-7-高级控件slider的使用
- cocos2d-x 界面编程二(cocostudio GUI的控件事件响应)
- Cocos2d-x3.1中的Text控件的使用
- unity3d插件Daikon Forge GUI 中文教程6-高级控件richtextlabel的使用
- cocos2d-x相关控件的使用
- GUI设计禁忌 之一 ——使用了错误的控件