Cocos2d-x學習筆記

Cocos2d-x學習筆記

A. Severalimportant concepts

Director: run backward, manage the all the scenes in the game like replacescene.

Scene: run at forward, contains several layers, control the layers,now a container of the layers.

Layer: run at forward, each layer contains several sprites, andcontrol the sprites, accept input from touching or accelerometer.

Sprite: a sprite is just a changing image.

 

B. Afterreading the HW code of Felix

1.About Scene: Scene classes are used to create scenes as container of layersimplemented by creating scenes at first then creating layers of this scene andadd them to the current scene.

2.About Layer: When we init a layer we create the menu items(menu) and spritesand add them to the layer itself.

3.About the control of menu: we set a selector(a function) to carry out the event we get fromthe menu. Maybe we use Director to call its member functions to replace thescenes.

 

C. Timer:

We use schedule(stepfun,interval)  inherited from CocosNode to run an actionrepeatly.

And we use unscheduled to release the timer.`                                                                                                                                           `

 

D.Action.

Instant Action: Place  Hide Show  ToggleVisibility

Finite Time Action: Moveto MoveBy JumptoJumpby …

Composition Ation: SequenceSpawnRepeat, Revers, Animation, RepeatForever, Back.

 

E. Cocos2dx Project settings in VS2008:

First we should build the libcocos2d, because we will use the outputof the libcocos2d building in our own project, so its setting should befinished first. 

Libcocos2d Project:

1.      Configuration Properties ->General -> Output Directory: the result(lib files) will be built to here soit should be a place we should know.

2.      Configuration Properties ->C/C++ -> General -> Additional Include Directories: here should be allthe head files’ directories that will be used by this project.

3.      Configuration Properties ->Linker -> General -> Output File: $(OutDir)\$(ProjectName).dll 

The OutDir is the same as the OutputDirectory in 1.

4.      Configuration Properties ->Linker -> General ->Additional Library Directories: $(OutDir)

5.      Configuration Properties ->Linker -> Input -> Additional Dependencies: put down all the libraries wewill use. Like : libEGL.lib libgles_cm.lib libxml2.lib libzlib.lib libpng.liblibjpeg.lib libiconv.lib pthreadVCE2.lib

 

My Cocos2d Project:

1.      Configuration Properties ->General -> Output Directory: this should be same with the one of Libcocos2dproject.

2. Configuration Properties-> C/C++ -> General -> Additional Include Directories:

Like:..\..\cocos2dx\include;..\..\cocos2dx;..\..\cocos2dx\platform;..\..\cocos2dx\platform\third_party\win32\OGLES.  We could add more if they will be use by ourproject just for improving developing speed.

3.      Configuration Properties ->Linker -> General -> Output File: $(OutDir)\$(ProjectName).exe 

4.      Configuration Properties ->Linker -> General ->Additional Library Directories: $(OutDir)

5.      Configuration Properties ->Linker -> Input -> Additional Dependencies: libcocos2d.lib libgles_cm.lib2

F.  After Writing My First Cocos2dx Program

1. Global initial settings isin the AppDelegate::initInstance andAppDelegate::ApplicationDidFinishLaunching, such as the windowsize and the FPSand so on.

2. Difference between JumpByand JumpTo.

JumpBy(Duration, Relative Increment to its StartPosition, Height,Jump Times).

JumpTo(Duration, Absolute Target Position to reach, Height, JumpTimes)

Code to Impliment Jump At the Original Place With CCJumpBy:

     //create a new sprite from an image

     CCSpritesprite=CCSprite::spriteWithFile("res\\image\\man.png");

     //set the sprite's start position

     sprite->setPosition(CCPointMake(s.width-40,s.height/2));

     //also add the sprite to the object of Layer1

     addChild(sprite);

     //create a jumpby action,jump 2 times in 1 second

     CCActionIntervaljump=CCJumpBy::actionWithDuration(1,CCPointMake(0,0),40,2);

     //create a repeat action to repeat the jump action

     CCActionrepeat=CCRepeatForever::actionWithAction(jump);

     //let sprite run the action

     sprite->runAction(repeat);

The same with MoveBy&MoveTo and ScaleBy&ScaleTo and so on.

XxxTo classes usually don’t support Reverse(), but XxxxBy classesusually support Reverse().

3.      some questions for theCCSequece:

Definition: 

classCC_DLLCCSequence : publicCCActionInterval { …};

classCC_DLLCCActionInterval : publicCCFiniteTimeAction { …};

That is to say CCSequence is a child class of CCActionInterval, butwhen I create a CCSequece:

CCActionIntervalsequence= (CCSequence::actions(scaleExpand,ScaleShrink, NULL));

The compiler complains:

 cannot convert from 'cocos2d::CCFiniteTimeAction *' to'cocos2d::CCActionInterval *'

I ‘solve’ this problem by cast it to CCActionInterval forcely andthe problem could run seems OK.

4. A good example ofCCSequence:

Example:

CCAction *pingPongAction =CCSequence::actions(action, action->reverse(), NULL);

G. Animation:

       1.create animation from several images

         //create an animation action

         CCAnimationanimition=CCAnimation::animation();

         //2 frames each second

         animition->setDelay(0.5f);

         //add the files as frames

         for(inti=0;i<NumOfImages;i++)

         {

                   animition->addFrameWithFileName(img[i].c_str());

         }

         CCAnimateanimate=CCAnimate::actionWithAnimation(animition);

         //create a repeat action to repeatthe rotate action

         CCActionrepeat=CCRepeat::actionWithAction(animate,2);

         //let sprite run the action

         sprite->runAction(repeat);

2.create animation fromone image

       //load the image

         CCTexture2D *PlayerRunTexture=CCTextureCache::sharedTextureCache()->

addImage(".\\res\\image\\swordman.png");

         //store the frames in theCCMutableArray

         CCMutableArray<CCSpriteFrame*>* animFramesnewCCMutableArray<CCSpriteFrame*>(11);

         for(inti=0;i<8;i++)

         {

                   animFrames->addObject(CCSpriteFrame::frameWithTexture

(PlayerRunTexture,CCRectMake(72*i,0,72,72)));

         }

         

         //create the animation object

         CCAnimationanimation=newCCAnimation();

         animation->initWithFrames(animFrames,0.08f);

         animFrames->release();

         //create animation action

         CCActionIntervalanimate=CCAnimate::actionWithAnimation(animation,false);

         //create a new sprite from an thefirst frame

         CCSpritesprite=CCSprite::spriteWithSpriteFrame(CCSpriteFrame::

frameWithTexture(PlayerRunTexture,CCRectMake(0,0,72,72)));

         //set the sprite's start position

         sprite->setPosition(CCPointMake(s.width/2,s.height-80));

         //also add the sprite to theobject of Layer1

         addChild(sprite);

         //create a repeat action to repeatthe jump action

         CCActionrepeat=CCRepeatForever::actionWithAction(animate);

         //let sprite run the action

         sprite->runAction(repeat);

3.create animation fromone png image and it’s .plist and .anim files(produced from its .sprite file).

 

HIntrinsic Animation of aSprite is the Animation which always exists with Sprite and keep running.

I. Don’t Know How to UseCCCallFun ?

         //create a function action

         CCCallFuncacf=CCCallFunc::actionWithTarget(this,callfunc_selector(Layer1::callback));

Use it as an action will be ok. But I madea stupid mistake as I forgot to set a BreakPointto test if the callback() has been runned.

J. Aboutthe CCSequece:

CCSequence cannot be directly used byCCRepeatForever(CCActionInterval*),Because the return type fromCCSequence::actions(CCFiniteTimeAction*,…) is CCFiniteTimeAction.CCActionInterval is inherited from CCFiniteTimeAction. Pointer to Parent classcannot be cast to Pointer to Child class. Instead we could useCCSequence::initOneTwo() several times to create our Sequece as its return typeis CCSequence which is a child class of CCActionInterval.

K. Howto test double-click in cocos2d-x?

There is no direct function to implementthis, but we can implement it by test the tick between two clicks.ssssss

L.MapEditor

We construct a map in the cocos2d-x gameswith tiles,because e will fill many places in the map with same tiles so, thissolution will save a lot memory.

The result of the TMX Editor:

  1. a .tmx file which is an XML file indicates the organization of the map from the tiles.
  2. a .png image provides the tiles we used before.

Display Map:  CCTMXTiledMap

Parse The XMLFiles: CCTMXXMLParser(not found in cocos2d-x).

How to use the Tile tool to edit map?

Elements:

Map, Layer, Tileset.

We can add properties which we will usewhen programming to each kind of element.

Save->.tmx, it is an XML file containsour map information.

We can also export the properties of ourtileset as an element which can be imported to other map.

 

About the XML file:

It’s structure like html contains manylabels indicates the elements we can see from the Tile tool.

 

Structure of the Game data:

The XML files gives the structure of ourscenes and maps, and some properties we can use to set our sprites.

The data can be got from database .dbfiles.

 

M. CCAnimationhas no constructor like CCAnimation().

       TodayI tried to create an animation with the following code:

                CCAnimationanimation=newCCAnimation();

       Butit leads to a crush, I don’t knowwhy, and compare the code with my code to create an animation yesterday, it islike this:

                       CCAnimationanimation=CCAnimation::animation();

       Somethingmust be wrong , I looked up the definition of the CCAnimation and didn’t find aCCAnimation() constructor but a static initial function animation().

 

NCoding tips when you compare a variable and a constant.

Putting the constant forward will reducethe chance you make mistakes because constant cannot be assigned.

Like: p is a pointer:

The error like this  If(NULL = p) will not pass the compile.

But this If(p = NULL) may lead to a crush.

 

O.usageof callFunctionND

Do something after some action.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章