游戏行旁布景类
主要分为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();}
游戏排行榜的效果图(分别是进击的小鸟和水果大逃亡界面) 总结
以上是内存溢出为你收集整理的cocos2dx休闲游戏--Fruit & Bird 开发学习(3)———RankBirdLayer全部内容,希望文章能够帮你解决cocos2dx休闲游戏--Fruit & Bird 开发学习(3)———RankBirdLayer所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)