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

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);

	}
}




  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值