您的位置:首页 > 移动开发 > Cocos引擎

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

}

}

程序运行如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: