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

来源:互联网 发布:淘宝店授权店 编辑:程序博客网 时间:2024/04/30 20:33

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


CannonLayer.h

#include "cocos2d.h"#include "Weapon.h"USING_NS_CC;class CannonLayer:public CCLayer{    public:    CREATE_FUNC(CannonLayer);    bool init();    //声明炮塔的整合类    CC_SYNTHESIZE_READONLY(Weapon*, _weapon, Weapon);    //转换炮塔类型的方法    void switchCannon(CCMenuItemSprite* sender);    //声明2个调整炮塔等级的按钮    CCMenuItemSprite* _subItem;    CCMenuItemSprite* _addItem;    };

CannonLayer.cpp

#include "CannonLayer.h"#include "StaticData.h"USING_NS_CC;bool CannonLayer::init(){    bool pRet = false;    do {        CC_BREAK_IF(!CCLayer::init());        CCSize winSize = CCDirector::sharedDirector()->getWinSize();        _weapon = Weapon::create();        _weapon->setPosition(winSize.width * 0.5 , _weapon->getCannon()->getCurCannonSprite()->getContentSize().height * 0.26);        this->addChild(_weapon);        //设置2个按钮一个是“+”  另一个是“-”        _subItem = CCMenuItemSprite::create(CCSprite::createWithSpriteFrameName(STATIC_DATA_STRING("button_sub")), CCSprite::createWithSpriteFrameName(STATIC_DATA_STRING("button_sub")), this, menu_selector(CannonLayer::switchCannon));        _addItem = CCMenuItemSprite::create(CCSprite::createWithSpriteFrameName(STATIC_DATA_STRING("button_add")), CCSprite::createWithSpriteFrameName(STATIC_DATA_STRING("button_add")), this, menu_selector(CannonLayer::switchCannon));                CCMenu* menu = CCMenu::create(_subItem,_addItem,NULL);        //添加两个按钮设置他们之间的间隔是多少,从_weapon对象中先找到炮塔对象(getCannon)再找到对应的精灵getCurCannonSprite(),再找到精灵的宽。        menu->alignItemsHorizontallyWithPadding(_weapon->getCannon()->getCurCannonSprite()->getContentSize().width + 10);                menu->setPosition(ccp(winSize.width * 0.5, _subItem->getContentSize().height * 0.5));        this->addChild(menu);        pRet = true;    } while (0);    return pRet;}void CannonLayer::switchCannon(cocos2d::CCMenuItemSprite *sender){    CannonType type = _weapon->getCannon()->getType();    if (sender == _addItem)    {        type++;    }    else if (sender == _subItem)    {        type--;    }    _weapon->getCannon()->setType(type);}


我们还要对GameScene类做如下修改

GameScene.h

#include "BackgroundLayer.h"

#include "FishLayer.h"

#include "CannonLayer.h"

USING_NS_CC;

class GameScene:publicCCScene

{

    

public:

    //CREATE_FUNC这个宏定义能够调用create()方法和init()方法,就是初始化的意思

    CREATE_FUNC(GameScene);

    //初始化时在这个方法中赋值

    bool init();

    //本类的析构方法

    ~GameScene();

    //载入资源的方法

    void preloadResources();

    CC_SYNTHESIZE_RETAIN(BackgroundLayer*, _backgroundLayer, BackgroundLayer);

    

    //声明鱼层类的对象

    CC_SYNTHESIZE_RETAIN(FishLayer*, _fishLayer, FishLayer);

    

    //声明炮塔层的对象

    CC_SYNTHESIZE_RETAIN(CannonLayer*, _cannonLayer, CannonLayer);

};





GameScene.cpp

bool GameScene::init()

{

    bool pRet = false;

    do {

        //调用父类的初始化方法,CC_BREAK_IF就是一个容错保护的方法

        CC_BREAK_IF(!CCScene::init());

        //调用预加载资源的方法

        this->preloadResources();

        //初始化背景层

        _backgroundLayer =BackgroundLayer::create();

        //将层添加到场景中

        this->addChild(_backgroundLayer);

        

        //初始化鱼的层

        _fishLayer = FishLayer::create();

        //将鱼的层添加到场景中

        this->addChild(_fishLayer);

        

        //创建炮塔层之后将其添加到场景中

        _cannonLayer =CannonLayer::create();

        this->addChild(_cannonLayer);


        pRet = true;

    } while (0);

    return pRet;

}






原创粉丝点击