coco2dx學習筆記


Cocos2dx學習筆記

 

學習cocos2dx將近一個月了,這個強大的引擎令到我差點就放棄了,不過在再次的堅持下,還是選擇繼續前行。

 

重新梳理了一下學習的過程,遇到的困難和可以應用的東西。

 

在開始莫過於創建新項目的頭痛了,在沒有游戲引擎經驗的情況下,我獨自走上了這條不歸路。

 

創建新項目:開始在菜單文件中新建項目,讓我吃了不少虧。一大堆的錯誤報告,無法包含各種文件。新手沒有按步驟的胡亂創建,耗費了好多天時間。

 

終於在后來找到了新建項目的方法,右擊coco2dx項目新建

 

 

 

創建了不用經常報錯的項目后,終於可以專心的學習cocos2dx強大的2d圖片處理功能了。

 

 

接着,我尋找了HelloCpp中,關於HelloWorldScene.cpp中每句代碼的含義,並在每句后面添加自己理解的注解。這是非常基本的,打好最簡單的基礎,才能看懂以后更復雜的代碼。

 

 

開始顯示第一張圖片

CCSprite * righetdoor = CCSprite::create("begin//right.png");

CC_BREAK_IF(!righetdoor);

righetdoor->setAnchorPoint(ccp(0,0));//設置錨點

righetdoor->setPosition(ccp(470,0));//設置位置

this->addChild(righetdoor,1);//將圖片顯示出來,左為(類紋理),右為顯示的層

接下來就是創建動作

CCSprite * logo = CCSprite::create("begin//logo.png");// 顯示logo

CC_BREAK_IF(!logo);

logo->setAnchorPoint(ccp(0.5,0.5));

logo->setPosition(ccp(470,325));

 

CCMoveTo* movelogo  = CCMoveTo::create(1.0f,ccp(940,325));//絕對坐標括移?動

//CCMoveBy為相對坐標移動

   CCRotateTo* rot = CCRotateTo::create(1.0f,180);//旋轉

 

CCRotateTo* rot1 = CCRotateTo::create(1.0f,0);//旋轉

CCMoveTo* movelogo2 = CCMoveTo::create(1.0f,ccp(470,325));//絕對坐標括移動

  CCHide* hide1 =CCHide::create();//隱藏

 

CCCallFunc*cf=CCCallFunc::create(this,callfunc_selector(HelloWorld::admation));/調用一個函數

 

CCFiniteTimeAction*seq1=CCSequence::create(movelogo,rot,rot1,movelogo2,hide1,cf,NULL);//將一個連串動作連起來

//CCRepeat* repeat = CCRepeat::create(seq1,3);//重復sep1三次

logo->runAction(seq1);

this->addChild(logo,2);

 

 

//CCMoveTo* movelogo = CCMoveTo::create(1.0f,ccp(940,325));//絕對坐標移動

//logo->runAction(movelogo);

//CCRotateTo* rot = CCRotateTo::create(1.0f,180);

//CCFiniteTimeAction* seq = CCSequence::create(movelogo,rot,NULL);//在2.04版本與2.1版本在ccsequence的引用有所不同 串聯動作

//CCFiniteTimeAction* spa = CCSpawn::create(movelogo,rot,NULL);////在2.04版本與2.1版本在CCSpawn的引用有所不同 並聯動作

 

//CCRepeat* repeat = CCRepeat::create(spa,3);////重復spa動作3次

 

//logo->runAction(repeat);

 

 

場景方面

 

 

創建場景

Menuscence.cpp 文件

 

#include "Menuscence.h"

 

 

USING_NS_CC;

 

CCScene* Menuscence::scene()

 {

 CCScene* scene=CCScene::create();

 Menuscence* layer=Menuscence::create();

 scene->addChild(layer);

 return scene;

 }

 

 

bool Menuscence::init()

{

}

 

Menuscence::Menuscence(void)

{

}

 

Menuscence::~Menuscence(void)

{

}

 

"Menuscence.h"

#pragma once

#include "cocos2d.h"

 

#include <cstring>

 

class Menuscence :

public cocos2d::CCLayer

{

private:

cocos2d::CCLabelTTF* output;

public:

static cocos2d::CCScene* scene();

virtual bool init();

CREATE_FUNC(Menuscence);

Menuscence(void);

~Menuscence(void);

};

 

替換場景

 

CCScene* scene = Menuscence::scene();//創建場景類(需要定義一個Menuscence在先)

CCTransitionSlideInL* transide = CCTransitionSlideInL::create(1.0f,scene);//左移堆出場景

CCDirector::sharedDirector()->replaceScene(transide);//替換當前場景

 

 

 

菜單控件

添加顏色控件,默認錨點為左下角。單顏色

CCLayerColor* color = CCLayerColor::create(ccc4(255,0,0,125),200,100);//創建顏色控件,ccc4最后一個才是透明度

color->setPosition(ccp(300,300));//值得注意的是,設置color的位置

this->addChild(color,2);

 

//兩種顏色控件支持顏色漸變

CCLayerGradient*grad=CCLayerGradient::create(ccc4(255,0,0,255),ccc4(255,255,0,255),ccp(1,1));

//可以設定顏色, 還可以設定方向

grad->changeWidthAndHeight(100,100);//指定寬度和高度

grad->setPosition(ccp(100,300));//指定位置

this->addChild(grad,2);

 

//文字顯示控件

CCLabelTTF* tupe = CCLabelTTF::create("hello","Arial",50);

tupe->setColor(ccc3(255,0,0));

tupe->setPosition(ccp(300,300));

this->addChild(tupe,2);

 

 

//創建文字菜單控件

CCLabelTTF* laber1 = CCLabelTTF::create("Srar","Arial",50);

laber1->setColor(ccc3(255,0,0));

 

CCLabelTTF* laber2 = CCLabelTTF::create("Next","Arial",50);

laber2->setColor(ccc3(255,0,0));

 

CCMenuItemLabel*menulabel=CCMenuItemLabel::create(laber1,this,menu_selector(Menuscence::start));//文本菜單項1

//在菜單中添加需要回調的函數名稱

menulabel->setPosition(ccp(470,325));

 

 

CCMenuItemLabel*menulabe2=CCMenuItemLabel::create(laber2,this,menu_selector(Menuscence::Next));//文本菜單項2

//在菜單中添加需要回調的函數名稱

menulabe2->setPosition(ccp(470,275));

 

CCMenuItemImage*item1=CCMenuItemImage::create("layer/unhit.png","layer/hit.png",this,menu_selector(Menuscence::start));//添加圖片按鈕

item1->setPosition(ccp(100,200));

 

CCMenu* menu1 = CCMenu::create(menulabel,menulabe2,item1,NULL);//添加菜單項

menu1->setPosition(ccp(0,0));

this->addChild(menu1,2);

 

output=CCLabelTTF::create("abc","Arial",50);

output->setPosition(ccp(100,100));

addChild(output);

 

 

顯示動畫

包含在其應用的函數中

 

this->Moveright();

this->Movehit();

 

CCAnimation*animation1=CCAnimationCache::sharedAnimationCache()->animationByName("workright");

 

CCSprite* ivll = CCSprite::create();

ivll->setPosition(ccp(200,300));

this->addChild(ivll);

 

ivll->runAction(CCRepeatForever::create(CCAnimate::create(animation1)));//動作1

 

void Menuscence::Moveright()

{

//1.這是包含多張圖片的動畫方法

char TFileNameIn[64];//需要定義全局變量

strcpy_s(TFileNameIn,("sprite/work/work01.png"));//需要包含#include <cstring>

 

 for (int i=0;i<CMD01_Nm;i++)

 {

TFileNameIn[16] = 0x30 + (i/10);

         TFileNameIn[17] = 0x31 + (i-((i/10)*10));

 frame[i] = CCSpriteFrame::create(TFileNameIn,CCRectMake(0,0,74,62));

 }

//2.單張圖片加載動畫方式

/* CCSpriteFrame * frame[CMD01_Nm] = {NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL};

for (int i=0;i<CMD01_Nm;i++)

 {

frame[i]=CCSpriteFrame::create("sprite/work/work01.png",CCRectMake(74*i,62*0,74,62));

 }          */

 

CCArray*frames=CCArray::create(frame[0],frame[1],frame[2],frame[3],frame[4],frame[5],frame[6],NULL);

 

CCAnimation* animation1 = CCAnimation::createWithSpriteFrames(frames,0.1f);//幀率參數,數據類型為浮點!!!不能用整數

 

 

CCAnimationCache::sharedAnimationCache()->addAnimation(animation1,"workright");

顯示單個動畫

//CCSpriteFrame*frame1=CCSpriteFrame::create("sprite/work/work01.png",CCRectMake(0,0,74,62));

 

//CCArray*frames=CCArray::create(frame1,frame2,frame3,frame4,frame5,frame6,frame7,frame8,frame9,  //顯示單幀

// frame10,NULL);

 

//CCAnimation* animation1 = CCAnimation::createWithSpriteFrames(frames,0.1f);//幀率參數,數據類型為浮點!!!不能用整數

 

//CCAnimate* animate = CCAnimate::create(animation1);

 

//CCSprite* work = CCSprite::create();

 

 

//work->setAnchorPoint(ccp(0.5,0.5));

 

//work->setPosition(ccp(200,300));

 

//this->addChild(work,2);

 

//work->runAction(CCRepeatForever::create(animate));

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM