欢迎界面主要有背景、标题、开始按钮、平台、英雄,实现在BackgroundLayer层

image.png

一、背景

资源中有五张背景图片,每次加载时随机选择一张图片作为背景

Size MyWinSize = Director::getInstance()->getVisibleSize();
int RandomNumber = CCRANDOM_0_1()*1000;
int BGI_Number = RandomNumber%5;
stage_number = 1;
NowStage = stage_number == 0 ? 2:stage_number-1;
LastStage = NowStage ==0 ? 2:(NowStage-1);
NextStage = NowStage == 2 ? 0:(NowStage+1);
// 随机选择游戏欢迎界面时的背景图片
switch(BGI_Number){
case 0:
Image_One = Sprite::create("image/bg/bg1.jpg");
Image_Two = Sprite::create("image/bg/bg1.jpg");
break;
case 1:
Image_One = Sprite::create("image/bg/bg2.jpg");
Image_Two = Sprite::create("image/bg/bg2.jpg");
break;
case 2:
Image_One = Sprite::create("image/bg/bg3.jpg");
Image_Two = Sprite::create("image/bg/bg3.jpg");
break;
case 3:
Image_One = Sprite::create("image/bg/bg4.jpg");
Image_Two = Sprite::create("image/bg/bg4.jpg");
break;
case 4:
Image_One = Sprite::create("image/bg/bg5.jpg");
Image_Two = Sprite::create("image/bg/bg5.jpg");
break;
default:
break;
}
Image_One->setPosition(MyWinSize.width/2,MyWinSize.height/2);
Image_Two->setPosition(MyWinSize.width/2 + Image_Two->getContentSize().width,MyWinSize.height/2);
this->addChild(Image_One,1);
this->addChild(Image_Two,1);
二、开始按钮

开始菜单会上下浮动,用到RepeatForever重复播放动作

//开始菜单
StartBtn = MenuItemSprite::create(
Sprite::create("image/uires_2.png"),
Sprite::create("image/uires_2.png"),
NULL,this ,menu_selector(BackgroundLayer::Start));
StartBtn->setPosition(MyWinSize.width/2,MyWinSize.height/2+8);
MoveTo* StartBtnMoveDown = MoveTo::create(2,Vec2(MyWinSize.width/2,MyWinSize.height/2-8));
MoveTo* StartBtnMoveUp = MoveTo::create(2,Vec2(MyWinSize.width/2,MyWinSize.height/2+5));
auto StartMoveSeq = Sequence::create(StartBtnMoveDown,StartBtnMoveUp,NULL);
auto StartMoveRepeat = RepeatForever::create(StartMoveSeq);
StartBtn->runAction(StartMoveRepeat);
menu = Menu::create(StartBtn,NULL);
menu->setPosition(0,0);
this->addChild(menu,2);
用MenuItemSprite创建的菜单回调函数menu_selector(BackgroundLayer::Start)实现点击后的逻辑

三、平台

// 对所有平台初始化
for(int i=0;i<3;i++)
{
stage_sprite[i] = Sprite::create("image/stage1.png");
}
stage_sprite[0]->setScaleX(30);
stage_sprite[0]->setPosition(Vec2(MyWinSize.width/2, stage_sprite[0]->getContentSize().height/4));
// 另两个平台的位置
for(int i = 1;i<3;i++)
{
stage_sprite[i]->setPosition(Vec2(MyWinSize.width + stage_sprite[i]->getScaleX()*stage_sprite[i]->getContentSize().width, stage_sprite[i]->getContentSize().height/2));
}

for(int i = 0;i<3;i++)
{
this->addChild(stage_sprite[i],3);
}
四、英雄

//添加英雄
My_Player.init();
My_Player.SetPosition(Vec2(MyWinSize.width/2,stage_sprite[0]->getContentSize().height/4*3));
this->addChild(My_Player.getSprite(),5);
My_Player.Stay();
英雄单独用一个类Player来实现动画和动作,在下一节讲解