Spine入門(Spine 是一款針對游戲的 2D 骨骼動(dòng)畫編輯工具) : http://www.tairan.com/archives/11066/
注意:首先是導(dǎo)入資源凡人,筆者使用的是Mac平臺(tái),使用Xcode開發(fā)工具,導(dǎo)入圖片資源時(shí)誊锭,經(jīng)常報(bào)錯(cuò)碍岔!說是找不到資源舔痪!挺納悶的司倚!后來才知道cocos2d的資源倒入是 “藍(lán)色文件夾”,而且還要勾選下圖的按鈕
筆者使用的環(huán)境是Mac媚媒,之前標(biāo)簽使用的是LabelTTF嗜逻,新版本是 Label,新舊版本創(chuàng)建標(biāo)簽:
// 舊版本使用是沒問題的缭召,新版本有警告
auto label01 = Label::create("this is the first label","Arial",36);
新版本的使用分開的形式才不會(huì)警告:
創(chuàng)建 標(biāo)簽
auto label01 = Label::create();
label01->setString("this is the first label");
label01->setSystemFontName("Arial");
label01->setPosition(320,180);
addChild(label01);
創(chuàng)建圖片時(shí)栈顷,cocos2d并不顯得友好:png 圖片的后綴名也要寫上,否者報(bào)錯(cuò)嵌巷!
創(chuàng)建一個(gè)背景圖(精靈)
Size size = Director::getInstance()->getVisibleSize();
Vec2 origin01 = Director::getInstance()->getVisibleOrigin();
auto *background = Sprite::create("background.png");
background->setPosition(size.width/2,size.height/2);
background->setScale(0.7f);
addChild(background);
創(chuàng)建一個(gè)菜單按鈕 和實(shí)現(xiàn)點(diǎn)擊
// 菜單按鈕
auto* pButton1 = MenuItemImage::create("button1.png", "button2.png", this, menu_selector(HelloWorld::menu));
auto buttonItem1 = Menu::create(pButton1, NULL);
buttonItem1->setPosition(Vec2(0, 0));
buttonItem1->setPosition(350, size.width/2 - 60);
buttonItem1->setScale(0.6f);
addChild(buttonItem1);
// 實(shí)現(xiàn)點(diǎn)擊方法(.cpp)
// 點(diǎn)擊方法
void HelloWorld::menu(cocos2d::Ref *pSender)
{
CCLOG("button pressed!!!");
}
// 聲明 方法(.h)文件
// 自定義一個(gè)點(diǎn)擊方法
void menu(cocos2d::Ref *pSender);
按鈕的另外一種創(chuàng)建 Button
在.h 文件 導(dǎo)入:
** #include "ui/CocosGUI.h"**
using namespace cocos2d::ui;
// 自定義方法
void menuClickSleep(Ref *pSender, TouchEventType type);
在.m 文件 :
//創(chuàng)建Button對象,在cocosGUI中的所有控件都繼承自Widget
Button* sleepButton = Button::create("homePicture/sleep_guoyu.png");
//設(shè)置標(biāo)題妨蛹,標(biāo)題屬性(字體大小、字體晴竞、顏色等)
sleepButton->setTitleText("Go");
sleepButton->setTitleFontSize(28);
//設(shè)置按鈕位置
sleepButton->setPosition(Vec2(80, 80));
//設(shè)置Tag值蛙卤,標(biāo)識(shí)按鈕,從Widget繼承的屬性噩死,作用與iOS中UIView的tag值類似颤难。
sleepButton->setTag(1002);
//設(shè)置按鈕大小
sleepButton->setScale(0.6f);
// 設(shè)置按鈕 監(jiān)聽事件
sleepButton->addTouchEventListener(this, toucheventselector(HomeController::menuClickSleep));
// 顯示按鈕
this->addChild(sleepButton);
//其中回調(diào)函數(shù)的格式為:(監(jiān)聽方法)
void HomeController::menuClickSleep(cocos2d::Ref* pSender, TouchEventType type)
{
Button *sender = (Button*)pSender;
log("%d",sender->getTag());
if (type ==ui::TouchEventType::TOUCH_EVENT_BEGAN) {
CCLOG("開始點(diǎn)擊。已维。行嗤。。垛耳。");
}
}
創(chuàng)建按鈕方式3
導(dǎo)入:
#include "cocos-ext.h"
USING_NS_CC_EXT;
// 寫法更加接近iOS 的UIButton
ControlButton *btn = ControlButton::create();
btn->addTargetWithActionForControlEvents(<#cocos2d::Ref *target#>, <#Handler action#>, <#cocos2d::extension::Control::EventType controlEvents#>)