cocos2d-x的初步学习十六之坦克大战三

上篇文章中我们简单了添加了地图,这篇文章中,我们重新写一个地图层,叫MapLayer,继承CCLayerColor,CCLayerColor可以修改层颜色,大小。OK,下面我们直接上代码:

MapLayer.h

class MapLayer :public cocos2d::CCLayerColor
{

public:
    
    virtual bool init();
    
    // there's no 'id' in cpp, so we recommend to return the class instance pointer
    static cocos2d::CCScene* scene();
    
    
    // preprocessor macro for "static create()" constructor ( node() deprecated )
    CREATE_FUNC(MapLayer);


    void initWithMap(int leve,int tKind,int numLife);

private:
    
    int _leve;
    
    cocos2d::CCTMXLayer* _bg1Layer;
    cocos2d::CCTMXLayer* _bg2Layer;

};
上面的代码中,我们定义一个公有的方法initwithMap,来初始化我们的地图,

MapLayer.cpp

bool MapLayer::init()
{
    //////////////////////////////
    // 1. super init first
    if ( !CCLayerColor::initWithColor(ccc4(0, 0, 0, 255), wSize.height, wSize.height))
    {
        return false;
    }
    
    
    return true;
}



//初始化map
void MapLayer::initWithMap(int leve, int tKind, int numLife)
{
    _leve = leve;

    
    CCTMXTiledMap *gameMap=CCTMXTiledMap::create(CCString::createWithFormat("map%d.tmx",leve)->getCString());
    
    //缩放比列
    float scale=wSize.height/gameMap->getContentSize().height;
    
    gameMap->setScale(scale);
    
    gameMap->setPosition(0,0);

    this->addChild(gameMap, 1);
    
    //获取地图里我们定义的地图层
    _bg1Layer=gameMap->layerNamed("bg1");
    _bg2Layer=gameMap->layerNamed("bg2");

    _bg2Layer->setVisible(false);
    
}

OK,地图层我们已经创建好了,那么下面我们在我们的场景中加入我们的地图层

GameScene.h

class GameLayer:public cocos2d::CCLayer
{
    
public:
    
    virtual bool init();
    
    // there's no 'id' in cpp, so we recommend to return the class instance pointer
    static cocos2d::CCScene* scene();
    
    // a selector callback
    void menuCloseCallback(CCObject* pSender);
    
    // preprocessor macro for "static create()" constructor ( node() deprecated )
    CREATE_FUNC(GameLayer);
    

    void initWithMapInformation(int leve,int status ,int life);

    void showLife(int numLife);

    void showLeve(int inLeve);

private:
    
    cocos2d::CCLabelTTF *_1plifeString;
    cocos2d::CCLabelTTF* _leveString;
    
    MapLayer *_mapLayer;
    
    
};

GameScene.cpp

//初始化地图信息
void GameLayer::initWithMapInformation(int leve, int status, int life)
{


    //开始音乐
    SimpleAudioEngine::sharedEngine()->playEffect("start.aif");
    
    
    //创建一个颜色层
    CCLayerColor *backColor=CCLayerColor::create(ccc4(192, 192, 192, 255));
    
    this->addChild(backColor,1);
    
    //精灵帧类,提高效率
    CCSpriteFrameCache *frameCache=CCSpriteFrameCache::sharedSpriteFrameCache();
    
    frameCache->addSpriteFramesWithFile("images.plist");
    

    //场景内一些精灵的创建
    CCSprite *ipLife=CCSprite::createWithSpriteFrameName("IP.png");
    ipLife->setPosition(ccp(30*2, wSize.height-50*2));
    ipLife->setScale(2.0f);
    this->addChild(ipLife,1);

    CCSprite *ipLifeIcok=CCSprite::createWithSpriteFrameName("p1.png");
    ipLifeIcok->setPosition(ccp(40, wSize.height-70*2));
    ipLifeIcok->setScale(1.0f);
    this->addChild(ipLifeIcok,1);
    this->showLife(life);
    
    CCSprite *flag=CCSprite::createWithSpriteFrameName("flag.png");
    flag->setPosition(ccp(wSize.width-100, wSize.height-200*2));
    flag->setScale(2.0f);
    this->addChild(flag,1);
    
    this->showLeve(leve);

    _mapLayer=MapLayer::create();
  
    _mapLayer->initWithMap(leve, status, life);
    
    _mapLayer->setPosition(ccp(wSize.width/6, 0));

    this->addChild(_mapLayer,1);

  
}


//显示life
void GameLayer::showLife(int numLife)
{

    if (_1plifeString!=NULL) {
        
    
    _1plifeString->removeFromParentAndCleanup(true);

    }
    _1plifeString=CCLabelTTF::create(CCString::createWithFormat("%d",numLife)->getCString(), "Courier-Bold", 40);
    _1plifeString->setColor(ccc3(0, 0, 0));
    _1plifeString->setPosition(ccp(45*2, wSize.height-70*2));
    this->addChild(_1plifeString,1);

}


//显示关卡
void GameLayer::showLeve(int inLeve)
{

    
    if (_leveString!=NULL) {
        
        
        _leveString->removeFromParentAndCleanup(true);
        
    }
    _leveString=CCLabelTTF::create(CCString::createWithFormat("%d",inLeve)->getCString(), "Courier-Bold", 40);
    _leveString->setColor(ccc3(0, 0, 0));
    _leveString->setPosition(ccp(wSize.width-100, wSize.height-230*2));
    this->addChild(_leveString,1);


}

在我们的游戏场景中,我们初始化了一些精灵,进行了一些界面的布局

cocos2d-x的初步学习十六之坦克大战三_第1张图片



你可能感兴趣的:(游戏,cocos2d,跨平台,cocos2d-x,坦克大战)