【cocos2d-x 3.7 飞机大战】 决战南海I (一) 开始界面

【cocos2d-x 3.7 飞机大战】 决战南海I (一) 开始界面,第1张

概述        好久没写过博客了,现在把刚做的游戏发上来吧,以后要注意更新博客啦~! 游戏截图 游戏整体结构图 第一步 在 AppDelegate 中设定游戏界面大小以及缩放方式 cocos2d-x3.7新生成的项目中,AppDelegate有默认的界面大小以及缩放方式,这里,我对其作出一些更改,使其适应本项目 Size frameSize = glview->getFrameSize();

好久没写过博客了,现在把刚做的游戏发上来吧,以后要注意更新博客啦~!

游戏截图



游戏整体结构图



第一步 在AppDelegate 中设定游戏界面大小以及缩放方式

cocos2d-x3.7新生成的项目中,AppDelegate有默认的界面大小以及缩放方式,这里,我对其作出一些更改,使其适应本项目

 Size frameSize = glvIEw->getFrameSize();		Size winSize=Size(450,750);		float wIDthRate = frameSize.wIDth/winSize.wIDth;	float heightRate = frameSize.height/winSize.height;	    if (wIDthRate > heightRate)    {        		glvIEw->setDesignResolutionSize(winSize.wIDth,winSize.height*heightRate/wIDthRate,ResolutionPolicy::NO_border);    }     else    { 		glvIEw->setDesignResolutionSize(winSize.wIDth*wIDthRate/heightRate,winSize.height,ResolutionPolicy::NO_border);    }

游戏背景图片大小为 450*750,所以,这里以背景图片为标准,设置不同的缩放比例


第二步 更改HelloWorld类,使其成为启动界面

自带的HelloWorld类里面并没有太多内容,只要将其删除即可,然后,设计主界面

HelloWorld();~HelloWorld();// there's no 'ID' in cpp,so we recommend returning the class instance pointerstatic cocos2d::Scene* createScene();// Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'ID' in cocos2d-iphonevirtual bool init();    // implement the "static create()" method manuallyCREATE_FUNC(HelloWorld);

在每一个场景中,一般都会出现上面的函数,以后在介绍其他类的时候,除非特殊情况,否则不再说明。


通过文章一开始的图片,我们可以看到,启动界面包含四个菜单项按钮,以及初始动画

	//预加载声音和图片	voID preLoadSoundAndPicture();	//开始游戏	voID startGame(Ref* pSender);	//高分记录	voID highscore(Ref* pSender);	//游戏说明	voID aboutGame(Ref* pSender);	//退出游戏	voID menuCloseCallback(cocos2d::Ref* pSender);	//启动界面动画	Animate* startMainAnimate();	//卸载不必要的资源	virtual voID onExit();	//响应键盘(主要针对AndroID)	voID onkeyreleased(EventKeyboard::KeyCode keyCode,Event* event);


下面这个变量可以不必在头文件中声明,我们后面介绍另一种方式
EventListenerKeyboard* m_Listener;



下面是cpp文件的实现

#include "HelloWorldScene.h"#include "TollgateOne.h"#include "scoreScene.h"#include "AboutGame.h"USING_NS_CC;HelloWorld::HelloWorld(){}HelloWorld::~HelloWorld(){	Director::getInstance()->getEventdispatcher()->removeEventListener(m_Listener);<span >	</span>//一定要记得在析构函数中移除}Scene* HelloWorld::createScene(){    // 'scene' is an autorelease object    auto scene = Scene::create();        // 'layer' is an autorelease object    auto layer = HelloWorld::create();    // add layer as a child to scene    scene->addChild(layer);    // return the scene    return scene;}// on "init" you need to initialize your instancebool HelloWorld::init(){    //////////////////////////////    // 1. super init first    if ( !Layer::init() )    {        return false;    }	//预加载声音 图片	preLoadSoundAndPicture();	//播放背景音乐	CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/game_start.mp3",true);        Size visibleSize = Director::getInstance()->getVisibleSize();    Vec2 origin = Director::getInstance()->getVisibleOrigin();	//加载背景	auto m_background = Sprite::createWithSpriteFrame(		SpriteFrameCache::getInstance()->getSpriteFrameByname("backgroundStartGame.jpg"));	m_background->setposition(Vec2(origin.x + visibleSize.wIDth / 2,visibleSize.height / 2));	m_background->setAnchorPoint(Vec2(0.5,0.5));	this->addChild(m_background);	//加载启动界面动画	auto startSprite = Sprite::createWithSpriteFramename("backgroundAnimate1.png");	startSprite->setposition(Vec2(origin.x + visibleSize.wIDth / 2,visibleSize.height / 2));	this->addChild(startSprite,1);	startSprite->runAction(this->startMainAnimate());    /////////////////////////////    // 2. add a menu item with "X" image,which is clicked to quit the program    //    you may modify it.	//开始游戏 按钮	auto tempStart1 = Sprite::createWithSpriteFramename("StartGame_nor.png");	auto tempStart2 = Sprite::createWithSpriteFramename("StartGame_touched.png");	auto startItem = MenuItemSprite::create(		tempStart1,tempStart2,CC_CALLBACK_1(HelloWorld::startGame,this)		);	//高分记录 按钮	auto tempscore1 = Sprite::createWithSpriteFramename("Gamescore_nor.png");	auto tempscore2 = Sprite::createWithSpriteFramename("Gamescore_touched.png");	auto highscoreItem = MenuItemSprite::create(		tempscore1,tempscore2,CC_CALLBACK_1(HelloWorld::highscore,this)		);	//游戏说明 按钮	auto tempHelp1 = Sprite::createWithSpriteFramename("GameHelp_nor.png");	auto tempHelp2 = Sprite::createWithSpriteFramename("GameHelp_touched.png");	auto aboutGameItem = MenuItemSprite::create(		tempHelp1,tempHelp2,CC_CALLBACK_1(HelloWorld::aboutGame,this)		);    //退出游戏 按钮	auto tempOver1 = Sprite::createWithSpriteFramename("GameOver_nor.png");	auto tempOver2 = Sprite::createWithSpriteFramename("GameOver_touched.png");	auto closeItem = MenuItemSprite::create(		tempOver1,tempOver2,CC_CALLBACK_1(HelloWorld::menuCloseCallback,this)		);    // create menu,it's an autorelease object	auto menu = Menu::create(startItem,highscoreItem,aboutGameItem,closeItem,NulL);	menu->alignItemsverticallyWithpadding(closeItem->getContentSize().height/2);	menu->setposition(Vec2(origin.x + visibleSize.wIDth / 2,visibleSize.height / 2));    this->addChild(menu,1);	//监听手机键盘	m_Listener = EventListenerKeyboard::create();	m_Listener->onkeyreleased = CC_CALLBACK_2(HelloWorld::onkeyreleased,this);	Director::getInstance()->getEventdispatcher()->addEventListenerWithSceneGraPHPriority(		m_Listener,this);    return true;}//预加载声音 图片voID HelloWorld::preLoadSoundAndPicture(){	//加载声音	CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/game_start.mp3");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/game_over.wav");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadBackgroundMusic("sound/BackgroundMusic.wav");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/achIEvement.mp3");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/bullet.wav");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/button.mp3");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/enemy1_down.wav");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/enemy2_down.wav");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/enemy3_down.wav");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/get_bomb.mp3");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/get_double_laser.mp3");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/out_porp.mp3");	CocosDenshion::SimpleAudioEngine::getInstance()->preloadEffect("sound/use_bomb.mp3");		//加载图片	SpriteFrameCache::getInstance()->addSpriteFramesWithfile("ui/background.pList");	SpriteFrameCache::getInstance()->addSpriteFramesWithfile("ui/plane.pList");}//开始游戏voID HelloWorld::startGame(Ref* pSender){	CocosDenshion::SimpleAudioEngine::getInstance()->stopBackgroundMusic();	Director::getInstance()->replaceScene(TollgateOne::createScene());}//高分记录voID HelloWorld::highscore(Ref* pSender){	Director::getInstance()->pushScene(		TransitionProgressRadialccw::create(1.0f,scoreScene::createScene()));}//游戏说明voID HelloWorld::aboutGame(Ref* pSender){	Director::getInstance()->pushScene(		TransitionJumpZoom::create(1.0f,AboutGame::createScene()));}voID HelloWorld::menuCloseCallback(Ref* pSender){	Director::getInstance()->end();#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)	exit(0);#endif}//启动界面动画Animate* HelloWorld::startMainAnimate(){	Vector<SpriteFrame*> vecStartAnimate;	for (int i = 0; i < 5; i++)	{		auto tempString = __String::createWithFormat("backgroundAnimate%d.png",i + 1);		auto tempAnimate = SpriteFrameCache::getInstance()->getSpriteFrameByname(tempString->getCString());		vecStartAnimate.pushBack(tempAnimate);	}	auto animate = Animate::create(Animation::createWithSpriteFrames(		vecStartAnimate,0.5f,-1));	return animate;}//卸载不必要的资源voID HelloWorld::onExit(){	Layer::onExit();	Director::getInstance()->getTextureCache()->removeUnusedTextures();}//响应键盘(主要针对AndroID)voID HelloWorld::onkeyreleased(EventKeyboard::KeyCode keyCode,Event* event){	if (keyCode == EventKeyboard::KeyCode::KEY_ESCAPE)		Director::getInstance()->end();}

之前一直用cocos2d-x2.2.6,现在换成3.7了,感觉变化挺大的。


游戏中用到的所有资源文件,会在最后上传。

总结

以上是内存溢出为你收集整理的【cocos2d-x 3.7 飞机大战】 决战南海I (一) 开始界面全部内容,希望文章能够帮你解决【cocos2d-x 3.7 飞机大战】 决战南海I (一) 开始界面所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1069113.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存