好久没写过博客了,现在把刚做的游戏发上来吧,以后要注意更新博客啦~!
游戏截图
![](https://box.kancloud.cn/2016-07-26_57972c2ac4884.jpg)
![](https://box.kancloud.cn/2016-07-26_57972c2b2e64f.jpg)
![](https://box.kancloud.cn/2016-07-26_57972c2b72726.jpg)
![](https://box.kancloud.cn/2016-07-26_57972c2bbb451.jpg)
![](https://box.kancloud.cn/2016-07-26_57972c2c2b752.jpg)
游戏整体结构图
![](https://box.kancloud.cn/2016-07-26_57972c2c765a7.jpg)
第一步 在 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 pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone
virtual bool init();
// implement the "static create()" method manually
CREATE_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 style="white-space:pre"> </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 instance
bool 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了,感觉变化挺大的。
游戏中用到的所有资源文件,会在最后上传。