? ? ? ? 好久沒寫過博客了,現在把剛做的游戲發上來吧,以后要注意更新博客啦~!
游戲截圖





游戲整體結構圖

第一步 在?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了,感覺變化挺大的。
游戲中用到的所有資源文件,會在最后上傳。