[2048源码分析-1]游戏架构

写在前面:接下来我会逐一介绍一些小游戏的开发,这些小游戏不是我写的,是我找来学习的,都是基于cocos2d-x开发的,有兴趣的朋友可以了解一下.

摘要:这一篇主要讲述2048这款游戏的游戏架构,以及开始场景和结束场景的一些介绍.


2048这款游戏主要包括以下三个Scene:

LoadingScene:加载界面,显示作者等信息的同时异步加载SpriteFrameCache.

GameScene:游戏主场景,该场景只有一个Layer.

GameLayer:网格棋盘,它包含16个Node.

Sprite:一格方块,包含一个背景Sprite和一个Label.

OverScene:游戏结束界面.


首先我们看LoadingScene.h


#ifndef _LoadingScene_H_
#define _LoadingScene_H_
#include "cocos2d.h"

USING_NS_CC;

class LoadingScene:public Scene{
public:
	CREATE_FUNC(LoadingScene);
	virtual bool init();
	void onEnter() override;
	void loadComplete(Texture2D *texture);
};
#endif

这里转一个onEnter()和init()区别的帖子:http://blog.csdn.net/yuyueliuliu/article/details/17122145


看一下LoadingScene.cpp

#include "LoadingScene.h"
#include "GameScene.h"

bool LoadingScene::init(){
	if(Scene::init()){
		return true;
	}else{
		return false;
	}
}

void LoadingScene::onEnter(){
	auto size=Director::getInstance()->getVisibleSize();
	//添加背景
	auto splash=Sprite::create("splash.png");
	splash->setPosition(Point(size.width/2,size.height/2));
	this->addChild(splash);

	//图片资源的异步加载
	TextureCache::getInstance()->addImageAsync("sucai.png",CC_CALLBACK_1(LoadingScene::loadComplete,this));

	
}

//图片加载完成后的回调函数
void LoadingScene::loadComplete(Texture2D *texture){
	//texture是加载完的图片资源
	SpriteFrameCache::getInstance()->addSpriteFramesWithFile("sucai.plist", texture);
	log("load complete");

	//界面切换
	auto gameLayer = GameScene::create();
	Director::getInstance()->replaceScene(TransitionFade::create(3.0f, gameLayer));
	CCSpriteFrameCache::getInstance()->addSpriteFramesWithFile("sucai.plist", texture);//利用这个SpriteFrameCache来创建所有的精灵
	
	auto gameScene = GameScene::create();
	Director::getInstance()->replaceScene(TransitionFade::create(3.0F, gameScene));
}
这里涉及到TextureCache和SpriteFrameCache。前面是对图片资源的预加载,加载好后通过回调函数loadingComplete加载SpriteFrameCache.这样以后Sprite的创建都通过SpriteFrameCache来进行。参考:http://www.cocoachina.com/cocos/20140507/8344.html

这里顺便说一下Cocos2d的三个缓存类,参考:  http://blog.csdn.net/star530/article/details/23612487


接下来是OverScene.cpp

#include "OverScene.h"
#include "GameScene.h"

Scene* OverScene::createScene(){
	auto scene=Scene::create();

	auto layer=OverScene::create();

	scene->addChild(layer);

	return scene;
}

bool OverScene::init(){
	if(!Layer::init()){
		return false;
	}

	Size size=Director::getInstance()->getVisibleSize();

	auto label=Label::create("GAME OVER","Arial",50);
	label->setPosition(Point(size.width/2,size.height/2));
	this->addChild(label);

	auto mLabel=Label::create("Restart","Arial",30);
	auto uiRestart=MenuItemLabel::create(mLabel,CC_CALLBACK_1(OverScene::restartMenu,this));
	uiRestart->setAnchorPoint(Point::ZERO);
	uiRestart->setPosition(Point::ZERO);
	auto menu=Menu::create(uiRestart,NULL);
	menu->setAnchorPoint(Point::ZERO);
	menu->setPosition(Point(10,10));
	this->addChild(menu);
	return true;
}

void OverScene::restartMenu(Ref *pSender){
	Director::getInstance()->replaceScene(TransitionFade::create(1.0f,GameScene::create()));
}

需要学习的是Menu的创建以及回调函数的使用。

你可能感兴趣的:(Cocos2d)