您的位置:首页 > 移动开发 > Cocos引擎

小小打灰机——1-制作游戏首界面和滚动背景

2017-09-16 18:38 369 查看


start_game.h

#ifndef START_GAME__H_
#define START_GAME__H_

#include "cocos2d.h"

USING_NS_CC;
using namespace std;

class StartGame : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();

virtual bool init();

Size visiblesize();

void start_play();

CREATE_FUNC(StartGame);

};

#endif

start_game.cpp
#include "start_game.h"
#include "main_game.h"

cocos2d::Scene* StartGame::createScene()
{
auto scene = Scene::create();
auto layer = StartGame::create();
scene->addChild(layer);
return scene;
}

bool StartGame::init()
{
if (!Layer::init())
return false;

//游戏背景
auto spr_pg = Sprite::create("ui/bg.png");
spr_pg->setPosition(visiblesize().width/2,visiblesize().height/2);
addChild(spr_pg);

auto dic = Dictionary::createWithContentsOfFile("fonts/text.xml");
auto str1 = (String*)(dic->objectForKey("title"));

auto title = Label::create();
title->setString(str1->getCString());
title->setSystemFontSize(60);
title->setPosition(visiblesize().width/2,visiblesize().height- title->getContentSize().height);
title->setColor(Color3B(174,111,23));
addChild(title);

//帧动画
auto animation = Animation::create();
for(int i=1; i<=4; i++)
{
auto spr_name = String::createWithFormat("ui/start%d.png",i);
animation->addSpriteFrameWithFile(spr_name->getCString());
}
animation->setDelayPerUnit(0.3f);
animation->setLoops(-1);//帧动画播放次数——为-1时无限循环
auto animate = Animate::create(animation);

auto zhensprite = Sprite::create("ui/start1.png");
zhensprite->setPosition(visiblesize().width/2, visiblesize().height/2);
addChild(zhensprite);
zhensprite->runAction(animate);

auto str2 = (String*)(dic->objectForKey("play"));
auto btn_label = Label::create();
btn_label->setString(str2->getCString());
btn_label->setSystemFontSize(40);

auto start_menu = MenuItemLabel::create(btn_label, CC_CALLBACK_0(StartGame::start_play, this));
start_menu->setPosition(visiblesize().width/2, visiblesize().height*0.3);
auto menu = Menu::create(start_menu, NULL);
menu->setPosition(Size::ZERO);
addChild(menu);

return true;

}

Size StartGame::visiblesize()
{
return Director::getInstance()->getWinSize();
}

void StartGame::start_play()
{
log("ss");
Director::getInstance()->replaceScene(TransitionFadeTR::create(0.5, MainGame::createScene()));
}



滚动背景的难点在于,首先要用两张相同图片进行y坐标移动。

另外,代码中使用到了onEnterTransitionDidFinish()和onExit()函数,这是我之前没有接触到的,并且还有一个与之差异的onEnter()

具体差异是:

onEnter()   是在进入场景的一瞬间就开始执行了。

onEnterTransitionDidFinish() 是在完全进入场景后开始执行的。

main_game.h

#ifndef MAIN_GAME__H_
#define MAIN_GAME__H_

#include "cocos2d.h"

USING_NS_CC;
using namespace std;

class MainGame : public cocos2d::Layer
{
public:
static cocos2d::Scene* createScene();

virtual bool init();

Size visiblesize();

void onEnterTransitionDidFinish();

void update(float t);

CREATE_FUNC(MainGame);

private:
Sprite* spr_bg1_;
Sprite* spr_bg2_;
};

#endif

main_game.cpp
#include "main_game.h"

cocos2d::Scene* MainGame::createScene()
{
auto scene = Scene::create();
auto layer = MainGame::create();
scene->addChild(layer);
return scene;
}

bool MainGame::init()
{

if (!Layer::init())
return false;

//背景1
spr_bg1_ = Sprite::create("ui/bgs.png");
spr_bg1_->setPosition(visiblesize().width/2 , visiblesize().height/2);
this->addChild(spr_bg1_);
//背景2
spr_bg2_ = Sprite::create("ui/bgs.png");
spr_bg2_->setPosition(ccp(visiblesize().width/2, spr_bg1_->getContentSize().height - 2));
this->addChild(spr_bg2_);

return true;
}

Size MainGame::visiblesize()
{
return Director::getInstance()->getWinSize();
}

void MainGame::onEnterTransitionDidFinish()
{
Layer::onEnterTransitionDidFinish();
scheduleUpdate();
}

void MainGame::update(float t)
{
//实现背景滚动
float y1 = spr_bg1_->getPositionY() - 3;
float y2 = spr_bg2_->getPositionY() - 3;

spr_bg1_->setPositionY(y1);
spr_bg2_->setPositionY(y2);

//判断背景图片是否超出边界
if (y1 < -spr_bg1_->getContentSize().height/2)
{
spr_bg1_->setPositionY(spr_bg2_->getPositionY() + spr_bg2_->getContentSize().height - 2);

}
else if (y2 < -spr_bg2_->getContentSize().height/2)
{
spr_bg2_->setPositionY(spr_bg1_->getPositionY() + spr_bg1_->getContentSize().height - 2);

}
}

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++ cocos2d 游戏