其實一直蠻想寫一個做項目的博文教程,但目前覺得自己能力還不夠,最近公司的項目又比較緊,一直都抽不出時間來寫關于項目的博文(明天我要上班呢\(^o^)/~)。對于新手朋友(其實我也是新手lol) 們來說,我覺得動手寫東西是一件提高水準的最好辦法了,有很多功能,總覺得自己有想法,很容易實作,但真正要實作起來會碰到各種各樣的問題,這些問題都是你之前沒有想到的,是以我在這裡還是建議新手朋友們一定要多多動手練習,隻有在動手練習中才能真正的提高自己的能力。
關于cocos2d-x的基礎知識,網上已經有一大堆博文了,而且那些大哥大姐們都寫得非常好,是以我也不需要重複造輪子了,我盡量能讓這篇博文能讓大家學習到在其他博文裡學不到的東西,這也是我寫這篇博文的初衷,本博文涉及到内容還蠻多的,肯定是不能一次性寫完,是以隻能有時間的時候再更新了。
本博文涉及到的知識:
1.定義場景
2.loading界面
3.菜單回調函數
4.進度條
5.場景切換(特效切換)
6.精靈動畫
7.滾動背景
8.聲音管理
下面就先上幾張效果
圖吧:
(⊙o⊙)… 十一點多了,明天還要上班呢,有時間再更新吧~~(~﹃~)~zZ
2013年7月25日21:58:22
繼續
我們先來走一個簡單的loading界面吧,l加載資源的操作我們最好都放在loading界面中加載,這樣在後續需要資源時可以直接從記憶體中讀取,加快遊戲的相應速度,我就不說那麼多了,直接上代碼吧:
h檔案:
include "cocos2d.h"
//#include "SoundManager.h"
#include "Box2D/Box2D.h"
#include "SimpleAudioEngine.h"
//#include "cocos-ext.h"
USING_NS_CC;
//USING_NS_CC_EXT ;
/************************************************************************/
/*
loading 界面
by 深圳-xo
*/
/************************************************************************/
class HelloWorld : public cocos2d::CCLayer
{
public:
virtual bool init();
static cocos2d::CCScene* scene();
void menuCloseCallback(CCObject* pSender);
//進度條
void progressAnimation();
void progressIsDone();
private:
//進度條所需
CCSprite* loading ;
CCSprite* loadingbg;
CCProgressTimer* pro;
CCProgressTo* to ;
CCFiniteTimeAction* action;
// implement the "static node()" method manually
CREATE_FUNC(HelloWorld);
};
using namespace cocos2d;
using namespace CocosDenshion;
CCScene* HelloWorld::scene()
{
CCScene * scene = NULL;
do
{
// 'scene' is an autorelease object
scene = CCScene::create();
CC_BREAK_IF(! scene);
// 'layer' is an autorelease object
HelloWorld *layer = HelloWorld::create();
CC_BREAK_IF(! layer);
// add layer as a child to scene
scene->addChild(layer);
} while (0);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
bool bRet = false;
do
{
CC_BREAK_IF(! CCLayer::init());
CCSprite* bg = CCSprite::create("menu/flash.png");
bg->setPosition(CCPointZero);
bg->setAnchorPoint(CCPointZero);
this->addChild(bg,-1);
this->progressAnimation();
bRet = true;
} while (0);
return bRet;
}
void HelloWorld::menuCloseCallback(CCObject* pSender)
{
// "close" menu item clicked
CCDirector::sharedDirector()->end();
}
/**進度條動畫的定義*/
void HelloWorld::progressAnimation()
{
CCSize size = CCDirector::sharedDirector()->getWinSize();
loading = CCSprite::create("loading.png");
pro = CCProgressTimer::create(loading);
// pro->setScale(0.5f);
//設定進度條顯示樣式
pro->setType(kCCProgressTimerTypeBar);
pro->setMidpoint(ccp(0,0));
pro->setBarChangeRate(ccp(1,0));
to = CCProgressTo::create(1,100);
action = CCSequence::create(to,CCCallFunc::create(this,callfunc_selector(HelloWorld::progressIsDone)),NULL);
loadingbg = CCSprite::create("loadbg.png");
// loadingbg->setScale(0.5f);
pro->setPosition(ccp(size.width/2,loadingbg->getContentSize().height-20));
loadingbg->setPosition(ccp(size.width/2,loadingbg->getContentSize().height-20));
this->addChild(loadingbg);
this->addChild(pro);
pro->runAction(action);
//schedule(schedule_selector(HelloWorld::progressIsDone),1);
}
void HelloWorld::progressIsDone()
{
if(to->isDone()||action->isDone())
{
//場景切換
CCTransitionScene* mScene = NULL ;
CCScene* s = MainMenu::scene();
float t = 1.2f ;
mScene = CCTransitionJumpZoom::create(t,s);
CCDirector::sharedDirector()->replaceScene(mScene);
}
}