怎么制作一个简单的游戏(3) Cocos2d-x 2.0.4
在第一篇《如何制作一个简单的游戏》和第二篇《如何制作一个简单的游戏(2)》基础上,增加游戏难度和关卡。原文《How To Make A Simple iPhone Game with Cocos2D 2.X Part 3》,在这里继续以Cocos2d-x进行实现。有关源码、资源等在文章下面给出了地址。
步骤如下:
1.使用上一篇的工程;
2.下载本游戏所需的资源,将资源放置"Resources"目录下:
3.创建不同类型的怪物。一种软弱的,但是移动快速的怪物,另一种强壮的,但是移动缓慢的怪物。现在创建一个Monster类,基类为CCSprite。同时也创建以Monster为基类的两种类型怪物类。右键工程,"Add"→"Class..."→"C++"→"Add","Base class"为CCSprite,"Class name"为Monster,如下图所示:
Monster.h文件代码为:
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#pragma once
#include "cocos2d.h" class Monster : public cocos2d::CCSprite { public: virtual bool initWithFile(const char *pszFilename, int hp, int minMoveDuration, int maxMoveDuration); protected: CC_SYNTHESIZE(int, _hp, Hp); CC_SYNTHESIZE(int, _minMoveDuration, MinMoveDuration); CC_SYNTHESIZE(int, _maxMoveDuration, MaxMoveDuration); }; class WeakAndFastMonster : public Monster { public: virtual bool init(void); CREATE_FUNC(WeakAndFastMonster); }; class StrongAndSlowMonster : public Monster { public: virtual bool init(void); CREATE_FUNC(StrongAndSlowMonster); }; |
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
#include "Monster.h"
bool Monster::initWithFile(const char *pszFilename, int hp, int minMoveDuration, int maxMoveDuration) { bool bRet = false; do { CC_BREAK_IF(! CCSprite::initWithFile(pszFilename)); this->_hp = hp; this->_minMoveDuration = minMoveDuration; this->_maxMoveDuration = maxMoveDuration; bRet = true; } while (0); return bRet; } bool WeakAndFastMonster::init(void) { return Monster::initWithFile("monster.png", 1, 3, 5); } bool StrongAndSlowMonster::init(void) { return Monster::initWithFile("monster2.png", 3, 6, 12); } |
|
#include "Monster.h"
|
2 3 4 5 6 7 8 9 10 |
//CCSprite *monster = CCSprite::create("monster.png");
Monster *monster = NULL; if (rand() % 2 == 0) { monster = WeakAndFastMonster::create(); } else { monster = StrongAndSlowMonster::create(); } |
2 |
int minDuration = monster->getMinMoveDuration(); //2.0;
int maxDuration = monster->getMaxMoveDuration(); //4.0; |
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
bool monsterHit = false;
CCArray *monstersToDelete = CCArray::create(); CCARRAY_FOREACH(_monsters, pObject2) { Monster *monster = (Monster*)pObject2; if (CCRect::CCRectIntersectsRect(projectile->boundingBox(), monster->boundingBox())) { monsterHit = true; monster->setHp(monster->getHp() - 1); if (monster->getHp() <= 0) { monstersToDelete->addObject(monster); } break; } } |
2 3 4 5 6 |
//if (monstersToDelete->count() > 0)
if (monsterHit) { projectilesToDelete->addObject(projectile); CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("explosion.wav"); } |
6.多个关卡。需要创建一个类,记录所有的信息来区分不同的关卡。在这个简单的游戏中,只需记录三种信息:关卡序号、出现敌人的间隔时间(更高关卡将更快的出现怪物)、背景颜色(可以任意地分辨不同的关卡)。创建Level类,派生自CCObject。Level.h文件代码为:
2 3 4 5 6 7 8 9 10 11 12 13 14 |
#pragma once
#include "cocos2d.h" class Level : public cocos2d::CCObject { public: virtual bool initWithLevelNum(int levelNum, float secsPerSpawn, cocos2d::ccColor4B backgroundColor); protected: CC_SYNTHESIZE(int, _levelNum, LevelNum); CC_SYNTHESIZE(float, _secsPerSpawn, SecsPerSpawn); CC_SYNTHESIZE(cocos2d::ccColor4B, _backgroundColor, BackgroundColor); }; |
2 3 4 5 6 7 8 9 10 |
#include "Level.h"
using namespace cocos2d; bool Level::initWithLevelNum(int levelNum, float secsPerSpawn, ccColor4B backgroundColor) { this->_levelNum = levelNum; this->_secsPerSpawn = secsPerSpawn; this->_backgroundColor = backgroundColor; return true; } |
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
#pragma once
#include "Level.h" class LevelManager : public cocos2d::CCObject { public: static LevelManager *sharedInstance(void); Level *curLevel(void); void nextLevel(void); void reset(void); bool init(); void end(); private: LevelManager(void); ~LevelManager(void); cocos2d::CCArray *_levels; int _curLevelIdx; }; |
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
#include "LevelManager.h"
using namespace cocos2d; LevelManager::LevelManager(void) { _levels = NULL; } LevelManager::~LevelManager(void) { if (_levels) { _levels->release(); _levels = NULL; } } LevelManager* LevelManager::sharedInstance(void) { static LevelManager *s_SharedLevelManager = NULL; if (!s_SharedLevelManager) { s_SharedLevelManager = new LevelManager(); s_SharedLevelManager->init(); } return s_SharedLevelManager; } Level * LevelManager::curLevel(void) { if (_curLevelIdx >= (int)_levels->count()) { return NULL; } return (Level*)_levels->objectAtIndex(_curLevelIdx); } void LevelManager::nextLevel(void) { _curLevelIdx++; } void LevelManager::reset(void) { _curLevelIdx = 0; } bool LevelManager::init() { _curLevelIdx = 0; Level *level1 = new Level(); level1->initWithLevelNum(1, 2, ccc4(255, 255, 255, 255)); level1->autorelease(); Level *level2 = new Level(); level2->initWithLevelNum(2, 1, ccc4(100, 150, 20, 255)); level2->autorelease(); _levels = CCArray::create(level1, level2, NULL); _levels->retain(); return true; } void LevelManager::end() { this->release(); } |
7.在HelloWorldScene.cpp文件中,添加头文件引用:
|
#include "LevelManager.h"
|
|
CC_BREAK_IF(! CCLayerColor::initWithColor(LevelManager::sharedInstance()->curLevel()->getBackgroundColor()));
|
|
this->schedule(schedule_selector(HelloWorld::gameLogic), LevelManager::sharedInstance()->curLevel()->getSecsPerSpawn());
|
8.接着,在GameOverLayer.cpp文件中,添加头文件引用:
|
#include "LevelManager.h"
|
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
bool GameOverLayer::initWithWon(bool won)
{ bool bRet = false; do { CC_BREAK_IF(! CCLayerColor::initWithColor(ccc4(255, 255, 255, 255))); CCString *message; if (won) { LevelManager::sharedInstance()->nextLevel(); Level *curLevel = LevelManager::sharedInstance()->curLevel(); if (curLevel) { message = CCString::createWithFormat("Get ready for level %d!", curLevel->getLevelNum()); } else { message = CCString::create("You Won!"); LevelManager::sharedInstance()->reset(); } } else { message = CCString::create("You Lose :["); LevelManager::sharedInstance()->reset(); } CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCLabelTTF *label = CCLabelTTF::create(message->getCString(), "Arial", 32); label->setColor(ccc3(0, 0, 0)); label->setPosition(ccp(winSize.width / 2, winSize.height / 2)); this->addChild(label); this->runAction(CCSequence::create(CCDelayTime::create(3), CCCallFunc::create(this, callfunc_selector(GameOverLayer::gameOverDone)), NULL)); bRet = true; } while (0); return bRet; } |
|
#include "LevelManager.h"
|
|
LevelManager::sharedInstance()->end();
|
参考资料:
1.How To Make A Simple iPhone Game with Cocos2D 2.X Part 3
http://www.raywenderlich.com/25806/harder-monsters-and-more-levels-how-to-make-a-simple-iphone-game-with-cocos2d-2-x-part-3
2.如何使用cocos2d来制作简单的iphone游戏:更猛的怪物和更多的关卡
http://www.cnblogs.com/zilongshanren/archive/2011/03/28/1997966.html
非常感谢以上资料的学习,本例子源代码附加资源下载地址:http://download.csdn.net/detail/akof1314/4885926
如文章存在错误之处,欢迎指出,以便改正。