cocos2d-x-3.2塔防游戏开发2:建塔、角度的旋转、发射箭

本文详细介绍了使用cocos2d-x开发塔防游戏的过程,包括创建箭塔的组件、放塔逻辑、触摸事件处理、塔的选择面板、子弹发射与碰撞检测,以及游戏中的金钱管理和UI元素如血条和金钱显示等关键步骤。通过这些内容,读者可以了解到如何在cocos2d-x环境中实现一个基本的塔防游戏核心功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、    创建箭塔

完成基类建设以后,接下来我们来创建一个最普通的炮塔——ArrowTower 箭塔。

   一个箭塔最起码应该由以下的三部分组成,1)箭塔底座,2)弓箭,3)子弹。如下图所示: 


2、       放塔的逻辑思路和过程:

   1.选中屏幕位置

2.判断该位置是否可以放塔

3.如果不可以放则显示 X 1秒钟

             如果可以放并且在当前则在用户选中的行列没有放过(此时需要一个map[r][c]来记录)

     显示3种可以选择的塔

     4.选择要放的塔

    5.在选中位置出现塔 (添加到图层 添加到集合 修改map[r][c]的标记)

6.建塔的面板


设定坐标

三个塔就是三个按钮,

•           autoImage= ImageView::create ("towerPos.png");

•           bt01=Button::create ("ArrowTower1.png");

•           bt02=Button::create ("AttackTower1.png");

•           bt02=Button::create ("MultiDirTower1.png");

•           使用Button 如果选中某种塔则在+位置创建

•           如果点击了屏幕其他位置整个Layout消失

 

3、    在GameScene中添加触摸侦听

.h文件定义

virtual bool onTouchBegan(Touch *touch, Event*unused_event); virtual bool onTouchBegan(Touch *touch, Event *unused_event);

.cpp中实现

//触摸

    auto listener=EventListenerTouchOneByOne::create();

    listener->onTouchBegan=CC_CALLBACK_2(GameScene::onTouchBegan,this);

   listener->onTouchMoved=CC_CALLBACK_2(GameScene::onTouchMoved,this);

   listener->onTouchEnded=CC_CALLBACK_2(GameScene::onTouchEnded,this);

      Director::getInstance()->getEventDispatcher()->addEventListenerWithSceneGraphPriority(listener, this);

   

 

 

bool GameScene::onTouchBegan(Touch *touch, Event*unused_event){

    if(this->getChildByTag(1001)!=NULL){

        this->removeChildByTag(1001); //1001是那个精灵。当我们每次点击的时候,我们就移除前一个塔的面板

       

    }

    //    CCLOG("点击了%f,%f",touch->getLocation().x,touch->getLocation().y);

    //   CCLOG("点击了地图的第%d,%d",(int)touch->getLocation().y/71,

    //                                    (int)touch->getLocation().x/71);

    nowRow=8-(int)(touch->getLocation().y/71);//确定点击的位置,tiled地图编辑器的0,0点是从左上角开始的

    nowCol=(int)(touch->getLocation().x/71);//确定你点击点得行列,71是我缩小之后算出的尺寸

    auto map=(TMXTiledMap*)this->getChildByTag(888);//得到地图

    //   CCLOG("点击了地图的第%d,%d,地图的编号是%d",nowRow,nowCol,map->getLayer("bg")->getTileGIDAt(Vec2(nowCol,nowRow)));

    bool canTouch=false;

    int tid=map->getLayer("bg")->getTileGIDAt(Vec2(nowCol,nowRow));//得到的是点下点得编号

    if(!map->getPropertiesForGID(tid).isNull()){    //获取属性

       auto tileTemp=map->getPropertiesForGID(tid).asValueMap();//获取属性的值

       if(!tileTemp.empty()){  //如果值不为空,就获取canTouch编号是1

           //tileTemp.at("canTouch").asInt();

           canTouch=true;                    //canTouch=1这里就可以放塔

           //CCLOG("这里可以放塔tidcanTouch=%d",tileTemp.at("canTouch").asInt());

       }

       

    }

   

    if (canTouch) {

       CCLOG("塔的选择面板");

       addTDSelect(8-nowRow,nowCol);//弹出箭塔的提示

    }else{

       //显示那个叉号

       auto tips=Sprite::createWithSpriteFrameName("no.png");//根据帧来添加一个精灵

       tips->setPosition(nowCol*71,(8-nowRow)*71);

       tips->setAnchorPoint(Vec2(0,0));

       this->addChild(tips);

       auto act=DelayTime::create(0.5);//必须加延迟否则就会刚产生就消失

       auto act1=CallFunc::create(CC_CALLBACK_0(Sprite::removeFromParent,tips));

       tips->runAction(Sequence::create(act,act1, NULL));

       

       

    }

    return true;

 

}

 

 

 

4、    弹出塔的选择面板

5、    GameScene.h中

void addTDSelect(int r,int c);//添加塔的选择面板

  GameScene.cpp中

void GameScene::addTDSelect(int r,int c){

   

    auto Image= Sprite::createWithSpriteFrameName("towerPos.png");//创建一个精灵

    int height=Image->getContentSize().height;

    int width=Image->getContentSize().width;

    auto  bt01= Sprite::createWithSpriteFrameName("ArrowTower1.png");

    auto  bt01_select= Sprite::createWithSpriteFrameName("ArrowTower1.png");

    bt01_select->setScale(1.1);

    auto  bt02= Sprite::createWithSpriteFrameName("AttackTower1.png");

    auto  bt02_select= Sprite::createWithSpriteFrameName("AttackTower1.png");

    bt02_select->setScale(1.1);

    auto  bt03= Sprite::createWithSpriteFrameName ("MultiDirTower1.png");

    auto  bt03_select= Sprite::createWithSpriteFrameName ("MultiDirTower1.png");

    bt03_select->setScale(1.1);

    //3Sprite转为Menu接收用户事件

    auto mitem01=MenuItemSprite::create(bt01,bt01_select, CC_CALLBACK_1(GameScene::selectTD, this));

    auto mitem02=MenuItemSprite::create(bt02,bt02_select, CC_CALLBACK_1(GameScene::selectTD, this));

    auto mitem03=MenuItemSprite::create(bt03,bt03_select, CC_CALLBACK_1(GameScene::selectTD,

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值