关卡

来源:互联网 发布:淘宝天猫退款多久到账 编辑:程序博客网 时间:2024/04/30 10:47

如何制作一个简单的游戏(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文件代码为:

1
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);
};
Monster.cpp文件代码为:
1
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"135);
}

bool StrongAndSlowMonster::init(void)
{
    return Monster::initWithFile("monster2.png"3612);
}
4.在HelloWorldScene.cpp文件中,添加头文件引用:
1
#include "Monster.h"
修改addMonster函数,改变创建怪物精灵的语句,如下代码:
1
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();
}
这样就有一半的机会创建不同类型的怪物。另外,我们定义了不同类型怪物的移动速度,故需更改移动时间,如下代码:
1
2
int minDuration = monster->getMinMoveDuration(); //2.0;
int maxDuration = monster->getMaxMoveDuration(); //4.0;
最后,修改update函数,更改_monsters循环的代码:
1
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;
    }           
}
每次击中怪物,减少它的一个HP血量,只有当小于等于0时,才被消灭。并且,记得跳出循环,因为每个子弹只能打中一只怪物。最后,更改projectilesToDelete语句为:
1
2
3
4
5
6
//if (monstersToDelete->count() > 0)
if (monsterHit)
{
    projectilesToDelete->addObject(projectile);
    CocosDenshion::SimpleAudioEngine::sharedEngine()->playEffect("explosion.wav");
}
5.编译运行,可以看到不同类型的怪物出现在屏幕上,如下图所示:

6.多个关卡。需要创建一个类,记录所有的信息来区分不同的关卡。在这个简单的游戏中,只需记录三种信息:关卡序号、出现敌人的间隔时间(更高关卡将更快的出现怪物)、背景颜色(可以任意地分辨不同的关卡)。创建Level类,派生自CCObject。Level.h文件代码为:
1
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);
};
Level.cpp文件代码为:
1
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;
}
接下去创建一个类,记录所有的关卡信息,以及当前处在哪个关卡。创建LevelManager类,派生自CCObject。LevelManager.h文件代码为:
1
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;
};
LevelManager.cpp文件代码为:
1
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(12, ccc4(255255255255));
    level1->autorelease();
    Level *level2 = new Level();
    level2->initWithLevelNum(21, ccc4(10015020255));
    level2->autorelease();
    _levels = CCArray::create(level1, level2, NULL);
    _levels->retain();
    return true;
}

void LevelManager::end()
{
    this->release();
}
用一个数组来记录关卡,一个索引指示当前关卡,以及一些方法,包括:获得当前关卡、进入下一个关卡、重置到第一个关卡。这个类是单例的,要通过sharedInstance方法来操作。
7.在HelloWorldScene.cpp文件中,添加头文件引用:
1
#include "LevelManager.h"
修改init函数,修改背景色的设置,如下代码:
1
CC_BREAK_IF(! CCLayerColor::initWithColor(LevelManager::sharedInstance()->curLevel()->getBackgroundColor()));
修改游戏逻辑定时器的安装,如下代码:
1
this->schedule(schedule_selector(HelloWorld::gameLogic), LevelManager::sharedInstance()->curLevel()->getSecsPerSpawn());
这样就能根据不同的关卡,来设定不同的出现敌人间隔时间。
8.接着,在GameOverLayer.cpp文件中,添加头文件引用:
1
#include "LevelManager.h"
initWithWon函数,修改成如下代码:
1
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(255255255255)));

        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(000));
        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;
}
9.最后,在AppDelegate.cpp文件,添加头文件引用:
1
#include "LevelManager.h"
AppDelegate析构函数中,添加如下代码:
1
LevelManager::sharedInstance()->end();
10.编译运行,就可以看到不同类型的怪物、多个关卡的游戏了,如下图所示:

参考资料:
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
如文章存在错误之处,欢迎指出,以便改正。

原创粉丝点击