(1)头文件
#include "cocos2d.h"
#include "cocos-ext.h"
using namespace cocos2d::gui;
#include "cocos-ext.h"
USING_NS_CC_EXT;
USING_NS_CC;
using namespace cocos2d::gui;
class HelloWorld : public cocos2d::CCLayer
{
public:
virtual bool init();
static cocos2d::CCScene* scene();
void menuCloseCallback(CCObject* pSender);
UILayer* mLayer;
void movementEventCallFuncTest(cocos2d::extension::CCArmature *armature, MovementEventType movementType, const char *movementID);
CREATE_FUNC(HelloWorld);
void testUILabel();
void testUIButton();
void touchEvent(CCObject *pSender, TouchEventType type);
void testUISlider();
void sliderEvent(CCObject *pSender, SliderEventType type);
void testUITextField();
void textFieldEvent(CCObject *pSender, TextFiledEventType type);
void testUICheckBox();
void selectedStateEvent(CCObject* pSender, CheckBoxEventType type);
void testLoadingBar();
void testUIListView();
cocos2d::CCArray* array;
};
(2)实现文件中#include "HelloWorldScene.h"
//UILayer
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;
}
mLayer=UILayer::create();
addChild(mLayer);
//UI编辑器使用方法
UILayout* layout=dynamic_cast
mLayer->addWidget(layout);
UIButton* btn=dynamic_cast
btn->setScale(3);
UIButton* btn2=dynamic_cast
btn2->setScale(6);
btn2->addTouchEventListener(this, toucheventselector(HelloWorld::touchEvent));
//动画编辑器使用方法
//this->addChild(<#cocos2d::CCNode *child#>)
/* UIScrollView* scrollView=UIScrollView::create();
scrollView->setTouchEnabled(true);
scrollView->setBounceEnabled(true);
scrollView->setSize(CCSize(winSize.width,winSize.height-200));
CCSize backgroundSize=CCSize(500,100);
scrollView->setPosition(ccp(0, 0));
mLayer->addWidget(scrollView);
for (int i=0; i<10; i++) {
UIImageView* imageView=UIImageView::create();
imageView->loadTexture("scrollbg.png");
imageView->setPosition(ccp(200, 200));
mLayer->addWidget(imageView);
// imageView->setTexture("");
// float innerWidth=scrollView->getcon
//CCScale9Sprite
// Create the imageview
UIImageView* imageView2 =UIImageView::create();
imageView2->setScale9Enabled(true);
imageView2->loadTexture("cocosgui/buttonHighlighted.png");
imageView2->setSize(CCSizeMake(200, 85));
imageView2->setPosition(ccp(widgetSize.width / 2.0f, widgetSize.height / 2.0f + imageView->getSize().height / 4.0f));
mLayer->addWidget(imageView2);
}*/
return true;
}
void HelloWorld::testUILabel()
{
UILabel* label=UILabel::create();
label->setText("UILabelText");
label->setFontName("Arial");
label->setColor(ccc3(255, 0, 0));
label->setPosition(ccp(80, 90+label->getSize().height));
mLayer->addWidget(label);
}
void HelloWorld::testUIButton()
{
UIButton* button=UIButton::create();
button->setTouchEnabled(true);
button->loadTextures("fight1.png", "fight2.png", "");
button->addTouchEventListener(this, toucheventselector(HelloWorld::touchEvent));
button->setTitleText("hhhhh");
button->setPosition(ccp(200, 200));
mLayer->addWidget(button);
}
void HelloWorld::touchEvent(CCObject *pSender, TouchEventType type)
{
switch (type)
{
case TOUCH_EVENT_BEGAN:
CCLog("aaaaaa");
break;
case TOUCH_EVENT_MOVED:
CCLog("bbbbbb");
break;
case TOUCH_EVENT_ENDED:
{
CCLog("cccccc");
//加载外在文件
CCArmatureDataManager::sharedArmatureDataManager()->addArmatureFileInfo("Guge0.png","Guge0.plist","Guge.ExportJson");
//创建一个相当于精灵的骨骼角name = DemoPlayer.ExportJson
CCArmature *armature=CCArmature::create("Guge");
armature->setPosition(ccp(240,160));
//设置缩放
armature->setScale(1.5f);
//吐槽下---如果是设置人物方向时,应该同时设置X,和Y方向
// armature->setScaleX(-1.2f); //X轴方向相反
//armature->setScaleY(1.2f);
//播放动画,armature->getAnimation()相当与获取一个骨骼动画管理器,里面有很多个动画
// armature->getAnimation()->playWithIndex(0);
//也可以使用,animation_name是自己在cocoStuiod中命名的动画名
armature->getAnimation()->play("Animation1");
//动画响应函数
armature->getAnimation()->setMovementEventCallFunc(this,movementEvent_selector(HelloWorld::movementEventCallFuncTest));
//设置动画帧之间的播放速度(频率)
armature->getAnimation()->setSpeedScale(0.3f);
addChild(armature,1,10);
}
break;
case TOUCH_EVENT_CANCELED:
CCLog("ddddddd");
break;
default:
break;
}
}
//循环结束会调用(三者只调用1种)
void HelloWorld:: movementEventCallFuncTest(cocos2d::extension::CCArmature *armature, MovementEventType movementType, const char *movementID) {
switch (movementType) {
case cocos2d::extension::START:
CCLog("start");
break;
case cocos2d::extension::COMPLETE:
CCLog("complete");
default:
break;
}
CCLog("%s",movementID);
//enum MovementEventType
//{
// START,动画开始
// COMPLETE,动画完成
// LOOP_COMPLETE 动画循环完成
//};
// movementID 是当前运行下的动画名
}
#pragma mark UISlider
void HelloWorld::testUISlider()
{
// Create the slider
UISlider* slider = UISlider::create();
slider->setTouchEnabled(true);
slider->loadBarTexture("cocosgui/sliderTrack.png");
slider->loadSlidBallTextures("cocosgui/sliderThumb.png", "cocosgui/sliderThumb.png", "");
slider->loadProgressBarTexture("cocosgui/sliderProgress.png");
slider->setPosition(ccp(200, 200));
// slider->setPosition(ccp(widgetSize.width / 2.0f, widgetSize.height / 2.0f + slider->getSize().height * 2));
// sliderAllow->setMinAllowPercent(20);
// sliderAllow->setMaxAllowPercent(80);
//
// sliderAllow->setTouchEnabled(true);
//sliderAllow->loadBarTexture("cocosgui/sliderTrack2.png");
slider->addEventListenerSlider(this, sliderpercentchangedselector(HelloWorld::sliderEvent));
mLayer->addWidget(slider);
}
void HelloWorld::sliderEvent(CCObject *pSender, SliderEventType type)
{
switch (type)
{
case ::cocos2d::gui::SLIDER_PERCENTCHANGED:
{
UISlider* slider = dynamic_cast
int percent = slider->getPercent();
CCLog(CCString::createWithFormat("Percent %d", percent)->getCString());
}
break;
default:
break;
}
}
#pragma mark UITextField
void HelloWorld::testUITextField()
{
// Create the textfield
UITextField* textField = UITextField::create();
textField->setMaxLengthEnabled(true);
textField->setPasswordEnabled(true);
textField->setPasswordStyleText("*");
textField->setMaxLength(3);
textField->setTouchEnabled(true);
textField->setFontName("Marker Felt");
textField->setFontSize(30);
textField->setPlaceHolder("input words here");
textField->setPosition(ccp(200, 200));
textField->addEventListenerTextField(this, textfieldeventselector(HelloWorld::textFieldEvent));
mLayer->addWidget(textField);
}
void HelloWorld::textFieldEvent(CCObject *pSender, TextFiledEventType type)
{
switch (type)
{
case TEXTFIELD_EVENT_ATTACH_WITH_IME:
{
UITextField* textField = dynamic_cast
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
textField->runAction(CCMoveTo::create(0.225,
ccp(screenSize.width / 2.0f, screenSize.height / 2.0f + textField->getContentSize().height / 2)));
}
break;
case TEXTFIELD_EVENT_DETACH_WITH_IME:
{
UITextField* textField = dynamic_cast
CCSize screenSize = CCDirector::sharedDirector()->getWinSize();
textField->runAction(CCMoveTo::create(0.175, ccp(screenSize.width / 2.0f, screenSize.height / 2.0f)));
}
break;
case TEXTFIELD_EVENT_INSERT_TEXT:
break;
case TEXTFIELD_EVENT_DELETE_BACKWARD:
break;
default:
break;
}
}
#pragma mark UICheckBox
void HelloWorld::testUICheckBox()
{
// Create the checkbox
UICheckBox* checkBox = UICheckBox::create();
checkBox->setTouchEnabled(true);
checkBox->loadTextures("cocosgui/check_box_normal.png",
"cocosgui/check_box_normal_press.png",
"cocosgui/check_box_active.png",
"cocosgui/check_box_normal_disable.png",
"cocosgui/check_box_active_disable.png");
checkBox->setPosition(ccp(240, 160));
checkBox->addEventListenerCheckBox(this, checkboxselectedeventselector(HelloWorld::selectedStateEvent));
mLayer->addWidget(checkBox);
}
void HelloWorld::selectedStateEvent(CCObject* pSender, CheckBoxEventType type)
{
switch (type)
{
case CHECKBOX_STATE_EVENT_UNSELECTED:
break;
case CHECKBOX_STATE_EVENT_SELECTED:
break;
default:
break;
}
}
#pragma mark UILoadingBar
void HelloWorld::testLoadingBar()
{
// Create the loading bar
UILoadingBar* loadingBar = UILoadingBar::create();
loadingBar->setName("LoadingBar");
loadingBar->loadTexture("cocosgui/sliderProgress.png");
loadingBar->setDirection(LoadingBarTypeRight);
loadingBar->setPercent(0);
loadingBar->setPosition(ccp(100, 100));
mLayer->addWidget(loadingBar);
/* m_nCount++;
if (m_nCount > 100)
{
m_nCount = 0;
}
UILoadingBar* loadingBar = dynamic_cast
loadingBar->setPercent(m_nCount);
loadingBar->setScale9Enabled(true);
loadingBar->setCapInsets(CCRectMake(0, 0, 0, 0));
loadingBar->setSize(CCSizeMake(300, loadingBar->getContentSize().height));*/
}
void HelloWorld::testUIListView()
{
array = CCArray::create();
CC_SAFE_RETAIN(array);
for (int i = 0; i < 20; ++i)
{
CCString* ccstr = CCString::createWithFormat("listview_item_%d", i);
array->addObject(ccstr);
}
// Create the list view
UIListView* listView = UIListView::create();
// set list view direction
listView->setDirection(SCROLLVIEW_DIR_VERTICAL);
listView->setTouchEnabled(true);
listView->setBounceEnabled(true);
listView->setBackGroundImage("cocosgui/green_edit.png");
listView->setBackGroundImageScale9Enabled(true);
listView->setSize(CCSizeMake(240, 130));
listView->setPosition(ccp(240,100));
// listView->addEventListenerListView(this, listvieweventselector(HelloWorld::selectedItemEvent));
//m_pUiLayer->addWidget(listView);
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
}