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

cocos2d(3.0)一些基础的东西

2014-04-23 17:22 218 查看
1.创建项目后环境配置:

附加目录:添加

$(EngineRoot)

$(EngineRoot)cocos

$(EngineRoot)cocos\editor-support

$(EngineRoot)build\Debug.win32

..\proj.win32

通用属性添加

(先从 cocos2d-x-3.0rc0 中 extensions cocos\editor-support cocos\ui 添加进去)

libcocosstudio

libExtensions

libGUI

链接器 附加依赖项:

libGUI.lib

libCocosStudio.lib

libExtensions.lib

头文件的添加:

这些都放在头文件里

#include "cocos2d.h"

#include "ui\CocosGUI.h"

#include "cocos-ext.h"

#include "ui\UIButton.h"

#include "cocostudio\CocoStudio.h"

#include "editor-support\cocostudio\CCSGUIReader.h"

#include <iostream>

using namespace std;

using namespace cocos2d;

using namespace cocostudio;

using namespace ui;

在init中就可以将外部建好的场景倒入进来

auto m_layout = cocostudio::GUIReader::getInstance()->widgetFromJsonFile("login_ui\\NewUI_1.ExportJson");

this->addChild(m_layout);

场景中的按钮和代码链接UI_BUTTON_LOGIN是在外部场景中的tag值

Button* startBtn = dynamic_cast<Button*>(m_layout->getChildByTag(UI_BUTTON_LOGIN));

startBtn->addTouchEventListener(this,toucheventselector(HelloWorld::touchButton));

场景中的中文字符的显示:

wstring HelloWorld::charToWstring(const char* c)

{

wstring ws;

int len = MultiByteToWideChar(CP_ACP,0,c,strlen(c),NULL,0);

wchar_t* m_wchar=new wchar_t[len+1];

MultiByteToWideChar(CP_ACP,0,c,strlen(c),m_wchar,len);

m_wchar[len]='\0';

ws.append(m_wchar);

return ws;

}

inline std::string WideByte2UTF8(const wstring& text)

{

int asciisize = ::WideCharToMultiByte(CP_UTF8, 0, text.c_str(), text.size(), NULL, 0, NULL, NULL);

if (asciisize == ERROR_NO_UNICODE_TRANSLATION ||

asciisize == 0)

{

return string();

}

char* resultstring = new char[asciisize];

int convresult = ::WideCharToMultiByte(CP_UTF8, 0, text.c_str(), text.size(), resultstring, asciisize, NULL, NULL);

if (convresult != asciisize)

{

return string();

}

std::string buffer(resultstring, convresult);

delete[] resultstring;

return buffer;

}

按钮切换场景

void HelloWorld::touchButton(Ref* obj,TouchEventType eventype)

{

Scene* pScene = ui_login_tag::createScene();

Director::sharedDirector()->replaceScene(pScene);

}

添加动画:

ArmatureDataManager::getInstance()->addArmatureFileInfo("MyAnimation.ExportJson");

Armature* armature = Armature::create("MyAnimation");

armature->setTag(AM_MYANIMATION);

armature->setPosition(Point(visibleSize.width/2,visibleSize.height/2));

this->addChild(armature);

按钮播放动画

auto armature = (Armature*)getChildByTag(AM_MYANIMATION);

switch (type)

{

case TouchEventType::TOUCH_EVENT_ENDED:

if(tag == UI_BUTTON_BUTTON_PLAY1)

{

armature->getAnimation()->play("hit");

}else if(tag == UI_BUTTON_BUTTON_PLAY2)

{

armature->getAnimation()->play("fall");

}

break;

default:

break;

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