cocos2dx休闲游戏--Fruit & Bird 开发学习(3)———RankBirdLayer

前端之家收集整理的这篇文章主要介绍了cocos2dx休闲游戏--Fruit & Bird 开发学习(3)———RankBirdLayer前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

游戏行旁布景类

主要分为RankBirdLayer和RankFruitLayer;这两个类的实现方法类似,在此处只列举一个RankBirdLayer类的实现。

首先是头文件的定义

#ifndef   __RANK_FRUIT_LAYER
#define  __RANK_FRUIT_LAYER


#include "cocos2d.h"
#include "GameSceneManager.h"

using namespace cocos2d;

class RankFruitLayer :public Layer
{
public:
	std::string  scoreFruit[5];//记录分数的字符串数组
	Label* labels;//声明一个指向分数的文本指针
	GameSceneManager* sceneManager;
public:
	virtual bool init();
	void save(int);//记录分数
	void load();//读取分数
	void menuCallBack0(Ref* pSender);//切换到主菜单场景
	void menuCallBack1(Ref* pSender);//切换到小鸟排行榜场景
	CREATE_FUNC(RankFruitLayer);
};
#endif

其次是源码的实现
#include "RankFruitLayer.h"
#include "RankBirdLayer.h"

using namespace std;
bool RankFruitLayer::init()
{
	if (!Layer::init())
		return false;

	//此处主要完成对界面的基本布局
	auto visibleSize = Director::getInstance()->getVisibleSize();
	auto origin = Director::getInstance()->getVisibleOrigin();
	auto backGround = Sprite::create("pic/morning.png");
	backGround->setAnchorPoint(Vec2::ZERO);
	backGround->setPosition(origin.x,origin.y + visibleSize.height - backGround->getContentSize().height);
	this->addChild(backGround);

	auto floor = Sprite::create("pic/floor.png");
	floor->setAnchorPoint(Vec2::ZERO);
	floor->setPosition(origin);
	this->addChild(floor);
	floor->runAction(RepeatForever::create(Sequence::create(MoveTo::create(0.5f,Vec2(-120,0)),MoveTo::create(0,Vec2::ZERO),nullptr)));

	auto rank = Sprite::create("pic/rankBackground.png");
	rank->setPosition(Vec2(270,530));
	this->addChild(rank,1);

	auto title = Sprite::create("pic/rankFruit.png");
	title->setPosition(Vec2(270,850));
	this->addChild(title,1);

	auto menuItemMenu = MenuItemImage::create("button/menu.png","button/menu_off.png",CC_CALLBACK_1(RankFruitLayer::menuCallBack0,this));
	menuItemMenu->setPosition(80,50);

	auto menuItemNext = MenuItemImage::create("button/back.png","button/back_off.png",CC_CALLBACK_1(RankFruitLayer::menuCallBack1,this));
	menuItemNext->setPosition(460,50);

	auto mn = Menu::create(menuItemMenu,menuItemNext,nullptr);
	mn->setPosition(Vec2::ZERO);
	this->addChild(mn,1);

//实现分数排行榜:排名--分数的形式
	int *tempFruit = new int[5];//创建一个整形数组,用于保存分数
	load();//读入分数
	for (int i = 0; i < 5; i++)
	{
		string score;
 		string num = Value(i + 1).asString();
		tempFruit[i] = Value(scoreFruit[i]).asInt();
		if (tempFruit[i] == 0)
			score = "-";
		else
			score = scoreFruit[i];
		labels = Label::createWithTTF(num,"fonts/FZKATJW.ttf",60);//labes指针指向 排名label
		rank->addChild(labels,2);
		labels->setPosition(90,280 - (50 * i));
		labels->enableOutline(Color4B(187,187,255),2);//给labels字符串描边
		labels = Label::createWithTTF(score,60);//labes指针指向 分数label
		rank->addChild(labels,2);
		labels->setPosition(315,2);
	}
	return true;
}


void RankFruitLayer::load()
{
	for (int i = 0; i < 5; i++)
	{
		scoreFruit[i] = UserDefault::getInstance()->getStringForKey(Value(i+1).asString().c_str(),"0");
	}
}

void RankFruitLayer::save(int newscore)
{
	string score;//记录分数
	string oldscore;//旧分数
	int *tempBird = new int[5];//创建一个整形数组,用来存储分数
	load();//把已有的分数读入scoreBird中(分数字符串)
	for (int i = 0; i < 5; i++)
		tempBird[i] = Value(scoreFruit[i]).asInt();//将分数字符串转换成对应的整形分数
	for (int i = 4; i >= 0; i--)
	{
		if (newscore >= tempBird[i])//新分数与最低所得分数进行比较,基本逻辑:新分数取代当前分数,当前分数下移
		{
			score = Value(newscore).asString();
			if (i != 4)
			{
				oldscore = scoreFruit[i];//将当前分数字符串用oldscore保存
				UserDefault::getInstance()->setStringForKey(Value(i + 1).asString().c_str(),oldscore);
			}
			UserDefault::getInstance()->setStringForKey((Value(i).asString()).c_str(),score);//如果
		}
		else
			break;//当新分数,不在大于tempBird[i],循环暂停
	}
}

void RankFruitLayer::menuCallBack0(Ref* pSender)//回到主场景
{
	sceneManager->goToMainScene();
}

void RankFruitLayer::menuCallBack1(Ref* pSender)//切换到下一个排行榜的回调方法
{
	sceneManager->goToRankBirdScene();
}

游戏排行榜效果图(分别是进击的小鸟和水果大逃亡界面) 原文链接:https://www.f2er.com/cocos2dx/338494.html

猜你在找的Cocos2d-x相关文章