cocos2d(3.0)一些基础的东西

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

场景中的button和代码链接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;

}

button切换场景

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

button播放动画

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;

 }

上一篇:Api管家系列(一):初探


下一篇:Java程序员入门:程序员究竟可以干多少年?