本节主要有3个类,LoadingScene,AnimatinoUtil和StartGame
先看AnimationUtil,代码如下,从木头的书里面搬过来的:
//AnimationUtil.h
#ifndef _ANIMATIONUTIL_H_
#define _ANIMATIONUTIL_H_
#include "cocos2d.h"
USING_NS_CC;
class AnimationUtil
{
public:
static Animation* createWithSingleFrameName(const char* name, float delay, int iLoops);
static Animation* createWithFrameNameAndNum(const char* name, int iNum, float delay, int iLoops);
};
#endif
//AnimationUtil.cpp
#include "AnimationUtil.h"
Animation* AnimationUtil::createWithSingleFrameName(const char* name, float delay, int iLoops)
{
SpriteFrameCache* cache = SpriteFrameCache::getInstance();
Vector<SpriteFrame*> frameVec;
SpriteFrame* frame = NULL;
int index = 0;
do
{
frame = cache->getSpriteFrameByName(StringUtils::format("%s%d.png", name, index++));
if (frame==NULL)
{
break;
}
frameVec.pushBack(frame);
} while (true);
Animation* animation = Animation::createWithSpriteFrames(frameVec);
animation->setDelayPerUnit(delay);
animation->setRestoreOriginalFrame(true);
animation->setLoops(iLoops);
return animation;
}
Animation* AnimationUtil::createWithFrameNameAndNum(const char* name, int iNum, float delay, int iLoops)
{
SpriteFrameCache* cache = SpriteFrameCache::getInstance();
SpriteFrame* frame = NULL;
Vector<SpriteFrame*> frameVec;
int index = 0;
for (index; index < iNum;index++)
{
frame = cache->getSpriteFrameByName(StringUtils::format("%s%d.png", name, index));
if (frame==NULL)
{
break;
}
frameVec.pushBack(frame);
}
Animation* animation = Animation::createWithSpriteFrames(frameVec);
animation->setDelayPerUnit(delay);
animation->setRestoreOriginalFrame(true);
animation->setLoops(iLoops);
return animation;
}
这个就是动画创建的辅助类,想想看,我们有三种不同颜色的小鸟,每只小鸟有一样的动画,如果给每只小鸟都创建动画的话,那么代码就重复了3次,我们本着尽量不让代码重复的原则,创建了这个辅助类。
接下来是LoadingScene,代码如下,看注释,挺好理解的.
//LoadingScene.h
#ifndef _LOADING_SCENE_H_
#define _LOADING_SCENE_H_
#include "cocos2d.h"
USING_NS_CC;
class LoadingScene :public Layer
{
public:
static Scene* createScene();
virtual bool init();
void loadingDone(float);
CREATE_FUNC(LoadingScene);
};
#endif
//LoadingScene.cpp
#include "LoadingScene.h"
#include "AnimationUtil.h"
#include "define.h"
#include "StartGame.h"
//创建场景
Scene* LoadingScene::createScene()
{
auto scene = Scene::create();
auto layer = LoadingScene::create();
scene->addChild(layer);
return scene;
}
//初始化
bool LoadingScene::init()
{
if (!Layer::init())
{
return false;
}
//加载图片帧缓存
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("bird.plist");
//预加载声音文件
CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sounds/sfx_die.mp3");
CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sounds/sfx_hit.mp3");
CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sounds/sfx_point.mp3");
CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sounds/sfx_swooshing.mp3");
CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sounds/sfx_wing.mp3");
//预加载3只不同颜色的小鸟的动画,并添加到动画帧缓存
Animation* animation0 = AnimationUtil::createWithSingleFrameName("bird0_", BIRD_FRE, -1);
Animation* animation1 = AnimationUtil::createWithSingleFrameName("bird1_", BIRD_FRE, -1);
Animation* animation2 = AnimationUtil::createWithSingleFrameName("bird2_", BIRD_FRE, -1);
AnimationCache::getInstance()->addAnimation(animation0, BIRDANIMATION_0);
AnimationCache::getInstance()->addAnimation(animation1, BIRDANIMATION_1);
AnimationCache::getInstance()->addAnimation(animation2, BIRDANIMATION_2);
//启动定时器,调用场景切换
this->schedule(schedule_selector(LoadingScene::loadingDone));
return true;
}
void LoadingScene::loadingDone(float)
{
//切换场景
Director::getInstance()->pushScene(StartGame::createScene());
}
这个场景主要用来预加载一些东西,像图片缓存动画缓存,声音等。
接下来我们看StartGame,显示开始游戏场景,场景有个按钮,点击进行开始游戏
//StartGame.h
#ifndef _START_GAME_H_
#define _START_GAME_H_
#include "cocos2d.h"
USING_NS_CC;
class StartGame:public Layer
{
public:
static Scene* createScene();
virtual bool init();
CREATE_FUNC(StartGame);
void gameStart();
};
#endif
//StartGame.cpp
#include "StartGame.h"
Scene* StartGame::createScene()
{
auto scene = Scene::create();
auto layer = StartGame::create();
scene->addChild(layer);
return scene;
}
bool StartGame::init()
{
if (!Layer::init())
{
return false;
}
auto winSize = Director::getInstance()->getWinSize();
//添加背景
auto bg = Sprite::createWithSpriteFrameName("bg_day.png");
bg->setPosition(Point(winSize/2));
this->addChild(bg);
//添加小鸟
auto bird = Sprite::createWithSpriteFrameName("bird0_0.png");
bird->setPosition(Point(winSize.width/2,winSize.height*0.8));
this->addChild(bird);
//添加游戏开始按钮
auto spriteBtn = Sprite::createWithSpriteFrameName("button_play.png");
auto playBtn = MenuItemSprite::create(spriteBtn, spriteBtn, CC_CALLBACK_0(StartGame::gameStart,this));
auto menu = Menu::create(playBtn, NULL);
menu->setPosition(Point(winSize / 2));
this->addChild(menu);
return true;
}
void StartGame::gameStart()
{
log("gameStart");
}
大家有没有发现一个define.h,那是什么东东?哈哈,那是一个宏定义而已,详见代码,我直接搬过来了
//define.h
#pragma once
#define BIRDANIMATION_0 "birdAnimation_0"
#define BIRDANIMATION_1 "birdAnimation_1"
#define BIRDANIMATION_2 "birdAnimation_2"
#define BIRD_FRE 0.1f
#define BIRD_RADIUS 15
#define LOADING_NUM 2
#define LOADING_FRE 0.5f
#define GRAVITY Point(0,-980)
#define VELOCITY Point(0,260)
#define THROUGH_HEIGHT 120
#define PIPE_UP 1001
#define PIPE_DOWN 1002
#define PIPE_RANGE 200
#define PIPE_X 300
#define PIPE_Y -170
#define PIPE_TIME 3.0f
#define PIPE_FRE 1.5f
#define PIPE_VELOCITY Point(-360,0)
#define LAND_VELOCITY 1.0/60
#define BIRD_DIE_ROTATION 90
#define SCORECARD_SHOW_TIME 0.6f
#define ADDSCORE_FRE 0.05f
#define CHANGESCENE_TIME 0.5f
#define MEDALS_0 5
#define MEDALS_1 10
#define MEDALS_2 15
#define MEDALS_3 20
这一期代码剧增,是不是吓到大家了,大家放心,认真看看,其实逻辑方面的基本没有,都是一些概念性的东西,有什么不懂的上网查一查就可以了,或者在这边留言,我会尽我所能解答的,当然太深奥的我也不懂。
本期运行的结果如下:
我将本期的代码上传,地址链接: http://pan.baidu.com/s/1ntHUYSD 密码: 4zb3
大家记得在AppDelegate.cpp里面导入头文件,LoadingScene.h,还有将auto scene = HelloWorld::createScene();改成auto scene = LoadingScene::createScene();哦。