Cocos2d-x 捕鱼达人游戏学习教程(5)--添加炮塔

Hi,推荐文件给你 "捕鱼达人练习例子(5).zip" http://vdisk.weibo.com/s/J1Xg8


在一篇文章的基础之上我们继续学习,这篇我们将添加炮塔。

我首先需要新建类:Bullet子弹类、Cannon炮塔类、FishingNet渔网类、Weapon武器类、CannonLayer炮塔层。

其中,Weapon武器类是Bullet、Cannon、FishingNet的管理员。

Bullet类代码实现如下:

Bullet.h




#include "cocos2d.h"
class Bullet : public cocos2d::CCNode
{
public:
    CREATE_FUNC(Bullet);
    bool init();
protected:
    cocos2d::CCSprite* _bulletSprite;
};




Bullet.cpp

#include "Bullet.h"
#include "StaticData.h"
USING_NS_CC;
bool Bullet::init()
{
    bool pRet = false;
    do {
        _bulletSprite = CCSprite::createWithSpriteFrameName(STATIC_DATA_STRING("bullet"));
        _bulletSprite->setAnchorPoint(ccp(0.5, 1.0));
        this->addChild(_bulletSprite);
        pRet = true;
    } while (0);
    return pRet;
}




我们接下来实现Cannon炮塔类

Cannon.h

#include "Bullet.h"
USING_NS_CC;
typedef enum {
    k_Cannon_Type_1 = 0,
    k_Cannon_Type_2,
    k_Cannon_Type_Count,
    k_Cannon_Type_Invalid
}CannonType;

class Cannon:public CCNode
{
    
public:
    Cannon();
    ~Cannon();
    
    static Cannon* create(CannonType type = k_Cannon_Type_1);
    bool init(CannonType type = k_Cannon_Type_1);
    CC_PROPERTY(CannonType, _type, Type);
    CCSprite* getCurCannonSprite();
    CCArray* _cannonSprites;
    
};



Cannon.cpp


#include "StaticData.h"
Cannon* Cannon::create(CannonType type)
{
    Cannon* cannon = new Cannon();
    cannon->init(type);
    cannon->autorelease();
    return cannon;
}

bool Cannon::init(CannonType type)
{
    bool pRet = false;
    do {
        _cannonSprites = CCArray::create();
        CC_SAFE_RETAIN(_cannonSprites);
        CCPoint anchorPoints = ccp(0.5, 0.26);
        for (int i = k_Cannon_Type_1; i < k_Cannon_Type_Count; i++)
        {
            CCString* fileName = CCString::createWithFormat(STATIC_DATA_STRING("cannon_level_name_format"),i + 1);
            CCSprite* cannonSprite = CCSprite::createWithSpriteFrameName(fileName->getCString());
            cannonSprite->setAnchorPoint(anchorPoints);
            _cannonSprites->addObject(cannonSprite);
        }
        this->setType(type);
        
        pRet = true;
    } while (0);    
    return pRet;
}



void Cannon::setType(CannonType type)
{
    if (_type != type)
    {
        if (type >= k_Cannon_Type_Count)
        {
            type = k_Cannon_Type_1;
        }
        else if (type < k_Cannon_Type_1)
        {
            type = (CannonType)(k_Cannon_Type_Count - 1);
        }
        this->removeChildByTag(_type, false);
        CCSprite* newCannonSprite = (CCSprite*)_cannonSprites->objectAtIndex(type);
        this->addChild(newCannonSprite, 0 ,type);
        _type = type;
    }
}



CCSprite* Cannon::getCurCannonSprite()
{
    return (CCSprite*)_cannonSprites->objectAtIndex(_type);
}

CannonType Cannon::getType()
{
    return _type;
}

Cannon::Cannon()
{
    _type = (CannonType)k_Cannon_Type_Invalid;
}
Cannon::~Cannon()
{
    CC_SAFE_RELEASE(_cannonSprites);
}



我们之后再来实现FishingNet渔网类


FishingNet.h

#include "cocos2d.h"
USING_NS_CC;

class FishingNet:public CCNode
{
    
public:
    bool init();
    CCSprite* _fishingNetSprite;
    
};

FishingNet.cpp

#include "StaticData.h"
bool FishingNet::init()
{
    bool pRet = false;
    do {
        _fishingNetSprite = CCSprite::createWithSpriteFrameName(STATIC_DATA_STRING("fishing_net"));
        this->addChild(_fishingNetSprite);
        pRet = true;
    } while (0);
    return pRet;
}

我们将在下一篇文章中实现对Weapon类的实现。


你可能感兴趣的:(游戏,类,String,cocos2d-x,捕鱼达人)