一、欢迎界面比较简单,由背景、标题、鸟、开始按钮、地面构成
1、背景
//通过时间判断是白天还是晚上加载不同的图片资源
time_t t = time(NULL);
tm* lt = localtime(&t);
int hour = lt->tm_hour;
//create the background image according to the current time;
Sprite *background;
if(hour >= 6 && hour <= 17){
background = Sprite::createWithSpriteFrameName("bg_day.png");
}else{
background = Sprite::createWithSpriteFrameName("bg_night.png");
}
background->setAnchorPoint(Point::ZERO);
background->setPosition(Point::ZERO);
this->addChild(background);
2、开始按钮
//add the start-menu to the current scene
Sprite *startButton = Sprite::createWithSpriteFrameName("button_play.png");
Sprite *activeStartButton = Sprite::createWithSpriteFrameName("button_play.png");
activeStartButton->setPositionY(5);
auto menuItem = MenuItemSprite::create(startButton,activeStartButton,NULL,CC_CALLBACK_1(WelcomeLayer::menuStartCallback, this));
menuItem->setPosition(Point(origin.x + visiableSize.width/2 ,origin.y + visiableSize.height*2/5));
//将menuItem菜单项加入到Menu菜单中
auto menu = Menu::create(menuItem,NULL);
menu->setPosition(Point(origin.x ,origin.y));
this->addChild(menu,1);
3、地面
地面的移动是通过两张图片拼接循环移动的效果
// Add the land
this->land1 = Sprite::createWithSpriteFrameName("land.png");
this->land1->setAnchorPoint(Point::ZERO);
this->land1->setPosition(Point::ZERO);
this->addChild(this->land1);
this->land2 = Sprite::createWithSpriteFrameName("land.png");
this->land2->setAnchorPoint(Point::ZERO);
this->land2->setPosition(this->land1->getContentSize().width - 2.0f, 0);
this->addChild(this->land2);
this->schedule(schedule_selector(WelcomeLayer::scrollLand), 0.01f);
4、挥动翅膀的小鸟
//create a bird and set the position in the center of the screen
this->bird = BirdSprite::getInstance();
this->bird->createBird();
this->bird->setTag(BIRD_SPRITE_TAG);
this->bird->setPosition(Point(origin.x + visiableSize.width / 2,origin.y + visiableSize.height*3/5 - 10));
this->bird->idle();
this->addChild(this->bird);
小鸟作为单独的类会在下一节讲解
项目源码托管在Github:https://github.com/smiger/FlappyBird