cocos2dx3.3开发FlappyBird总结十:背景层设计
发布时间:2020-12-14 20:38:48 所属栏目:百科 来源:网络整理
导读:游戏背景层的任务是很简单的,只是根据当前时间来显示白天或者黑夜背景图,提供获取地面的高度方法。 #ifndef __EngryBird__BackgroundLayer__#define __EngryBird__BackgroundLayer__#include "cocos2d.h" /** * The game background,showing the backgroun
游戏背景层的任务是很简单的,只是根据当前时间来显示白天或者黑夜背景图,提供获取地面的高度方法。 #ifndef __EngryBird__BackgroundLayer__
#define __EngryBird__BackgroundLayer__
#include "cocos2d.h"
/** * The game background,showing the background information * in the game. */
class BackgroundLayer : public cocos2d::Layer {
public:
/**
* The default constructor
*/
BackgroundLayer();
/** * The default destructor */
~BackgroundLayer();
/** * The init method,will init the super init method first * * @return true if succeeded,otherwise false */
virtual bool init();
CREATE_FUNC(BackgroundLayer);
/** * Get the land sprite height * * @return The height of land */
static float getLandHeight();
};
#endif /* defined(__EngryBird__BackgroundLayer__) */
背景层的实现中,看看怎么判断是不是白天或者黑夜。 bool BackgroundLayer::init() {
if (!Layer::init()) {
return false;
}
// Get the current time,judge whether now is day or night
time_t t = time(NULL);
tm *localTime = localtime(&t);
int hour = localTime->tm_hour;
std::string bgName;
if (hour >= 6 && hour <= 17) {
bgName = "bg_day";
} else {
bgName = "bg_night";
}
auto bgSprite = Sprite::createWithSpriteFrame(AtlasLoader::getInstance()->getSpriteFrame(bgName));
// 这里设置成(0,0),就可以从左下角开始显示至全屏
bgSprite->setAnchorPoint(Vec2::ZERO);
bgSprite->setPosition(Vec2::ZERO);
this->addChild(bgSprite);
return true;
}
获取地面的高度: // 其实就是加载地面精灵,然后获取其内容大小的高度
float BackgroundLayer::getLandHeight() {
auto spriteFrame = AtlasLoader::getInstance()->getSpriteFrame("land");
auto land = Sprite::createWithSpriteFrame(spriteFrame);
return land->getContentSize().height;
}
下一步,说说控制层(选项层) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 在Swift寻求“退出”等价物
- flex的dropdownlist在alert .show之后无法关闭下拉框的解决
- Oracle APEX 系列文章2:在阿里云上打造属于你自己的APEX完
- 正则表达式 – Vim Markdown突出显示(列表项和代码块冲突)
- [ERROR] XML Parser Error on line 11: 注释中不允许出现字
- xml 格式化输出
- oracle 中删除表 drop delete truncate
- ruby – 在没有attr_accessible的Rails 4中自记录ActiveRec
- ruby-on-rails – Rails 4.1.6会话ID
- c# – 如何在ASP.NET中编写内存使用情况日志