Principle:
Cocos2d has a director to control the whole game process, the director will add the scene to the screen, the scene has a variety of actors.
First by showing a picture to see the cocos2d game process:
AppDelegate.cpp
BOOL Appdelegate::applicationdidfinishlaunching ()
{
//Initialize director
Ccdirector *pdirector = ccdirector:: Shareddirector ();
Pdirector->setopenglview (Cceglview::sharedopenglview ());
Open fps
pdirector->setdisplaystats (true);
Set fps the ' default value is 1.0/60 if/don ' t call this
pdirector->setanimationinterval (1.0/60);
Create a scene
ccscene *pscene = Gamescence::scene ();
Run this scene
Pdirector->runwithscene (pscene);
return true;
}
The above code adds a scene gamescence, and here's a look at the concrete implementation:
GameScence.h
#include "cocos2d.h"
#include "box2d/box2d.h"
class Gamescence:public Cocos2d::cclayer
{public
:
BOOL Init ();
You must override the scene ()
static cocos2d::ccscene* scene ();
Equivalent to the CREATE function, is to rewrite the CREATE function in the Cclayer
Create_func (gamescence);
GameScence.cpp
#include "GameScene.h"
using namespace cocos2d;
ccscene* Gamescence::scene ()
{
ccscene * scene = NULL;
Do
{
scene=ccscene::create ();
gamescence* gamescene=gamescence::create ();
Scene->addchild (Gamescene);
} while (0);
return scene;
};
BOOL Gamescence::init ()
{
bool BRet = false;
Do
{
//Create a sprite from the picture
ccsprite* psprite = ccsprite::create ("Bg.png");
Gets the screen size
ccsize size = Ccdirector::shareddirector ()->getwinsize ();
Sets the position of the sprite in the scene, coordinates from the lower left corner of the 0,0
psprite->setposition (CCP (SIZE.WIDTH/2, SIZE.HEIGHT/2));
Add Sprite to Scene
This->addchild (psprite, 0);
} while (0);
Bret=true;
return bRet;
};