原理:web
Cocos2D中有個導演控制整個遊戲流程,導演將場景添加到屏幕上,場景中有各類各樣的演員。app
先經過顯示一張圖片來看看Cocos2D遊戲的流程:函數
AppDelegate.cpp
oop
[cpp] view plaincopy動畫
<EMBED id=ZeroClipboardMovie_1 height=18 name=ZeroClipboardMovie_1 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=1&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">this
bool AppDelegate::applicationDidFinishLaunching() spa
{ .net
// 初始化導演 orm
CCDirector *pDirector = CCDirector::sharedDirector(); blog
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
// 打開FPS
pDirector->setDisplayStats(true);
// 設置FPS the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);
// 建立一個場景
CCScene *pScene = GameScence::scene();
// 運行此場景
pDirector->runWithScene(pScene);
return true;
}
上邊的代碼添加了一個場景GameScence,下面看看具體實現:
GameScence.h
[cpp] view plaincopy
<EMBED id=ZeroClipboardMovie_2 height=18 name=ZeroClipboardMovie_2 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=2&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
#include "cocos2d.h"
#include "Box2D/Box2D.h"
class GameScence : public cocos2d::CCLayer
{
public :
bool init();
//必須重寫scene()
static cocos2d::CCScene* scene();
//至關於create函數,是重寫了CCLayer裏的create函數
CREATE_FUNC(GameScence);
};
GameScence.cpp
[cpp] view plaincopy
<EMBED id=ZeroClipboardMovie_3 height=18 name=ZeroClipboardMovie_3 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=3&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
#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
{
//從圖片建立一個精靈
CCSprite* pSprite = CCSprite::create("bg.png");
//獲取屏幕大小
CCSize size = CCDirector::sharedDirector()->getWinSize();
// 設置精靈在場景中的位置,座標從左下角0,0
pSprite->setPosition(ccp(size.width/2, size.height/2));
// 添加精靈到場景中
this->addChild(pSprite, 0);
}while(0);
bRet=true;
return bRet;
};
代碼中有註釋,就不在重複敘述了,
下面是效果圖:
怎麼讓其顯示一個動態的精靈呢,下面是修改後的init()
[cpp] view plaincopy
<EMBED id=ZeroClipboardMovie_4 height=18 name=ZeroClipboardMovie_4 type=application/x-shockwave-flash align=middle pluginspage=http://www.macromedia.com/go/getflashplayer width=18 src=http://static.blog.csdn.net/scripts/ZeroClipboard/ZeroClipboard.swf wmode="transparent" flashvars="id=4&width=18&height=18" allowfullscreen="false" allowscriptaccess="always" bgcolor="#ffffff" quality="best" menu="false" loop="false">
bool GameScene::init()
{
bool bRet = false;
do
{
CCSprite* pMap = CCSprite::create("bg.png");
CCSize size = CCDirector::sharedDirector()->getWinSize();
pMap->setPosition(ccp(size.width/2, size.height/2));
this->addChild(pMap, 0);
CCSprite* sprite ;
CCArray* pSpriteArray=CCArray::createWithCapacity(4);
for(int i=0;i<4;i++)
{
//截取frame
CCSpriteFrame* pFrame =CCSpriteFrame::create("role.png",CCRectMake(i*82,62*2,82,62));
pSpriteArray->addObject(pFrame);
//將精靈添加到場景,默認第一幀圖片
if(i==0){
sprite= CCSprite::createWithSpriteFrame(pFrame);
sprite->setPosition(ccp(200,100));
addChild(sprite);
}
}
//從array建立動畫
CCAnimation *splitAnimation=CCAnimation::createWithSpriteFrames(pSpriteArray,0.1f);
sprite->runAction(CCRepeatForever::create(CCAnimate::create(splitAnimation)));
}while(0);
bRet=true;
return bRet;
};
製做動態的圖片麻煩,就直接貼圖了: