cocos2d(3.0)一些基礎的東西

來源:互聯網
上載者:User

標籤:width   檔案   os   art   代碼   io   

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

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.