关卡
来源:互联网 发布:淘宝天猫退款多久到账 编辑:程序博客网 时间:2024/04/30 10:47
如何制作一个简单的游戏(3) Cocos2d-x 2.0.4
分类: 移动开发/ IOS/ 文章
在第一篇《如何制作一个简单的游戏》和第二篇《如何制作一个简单的游戏(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
#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
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);
}
2
3
4
5
6
7
8
9
10
Monster *monster = NULL;
if (rand() % 2 == 0)
{
monster = WeakAndFastMonster::create();
}
else
{
monster = StrongAndSlowMonster::create();
}
2
int maxDuration = monster->getMaxMoveDuration(); //4.0;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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 (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
#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
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
#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
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文件中,添加头文件引用:
8.接着,在GameOverLayer.cpp文件中,添加头文件引用:
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 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;
}
参考资料:
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
如文章存在错误之处,欢迎指出,以便改正。
- 关卡
- 关卡设计
- 关卡流
- 关卡重载
- 关卡设计入门
- 关于关卡的分类
- 关卡设计的基本理论
- 浅谈游戏关卡设计
- 浅谈游戏关卡设计
- 暂定的关卡数据格式
- java CyclicBarrier(关卡)详解
- 关卡设计理论
- 记一次关卡设计
- 设计游戏关卡
- 关卡原型制作思路
- 关卡原型制作思路
- 九、关卡数据
- Unity3D-Streaming关卡
- Hbase shell 常用命令
- 影响算法世界的十位大师
- 快速排序,两种实现
- 2013-4-1 网易邮箱 || java开发工程师笔试题
- 我所积累的20条编程经验
- 关卡
- 文学乐(http://www.wx6.org/)
- linux下Oracle通过设置大内存页解决使用swap分区问题
- 黑马程序员_流程控制
- 关于image标签无法获取本地图片
- 如何让你的简历不被pass?
- [跟着hsp步步学习系统]oracle培训学习集锦全360度扫描(4)
- Thinking in Java 第四版本--第3章练习题答案--for,break,switch,case,default
- 题目1128:求平均年龄