Cocos2d-x   发布时间:2022-05-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了Cocos3.4 横版游戏制作-《KillBear》-添加血条 攻击按键大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。
资源为网上寻找的,仅研究学习用,若是侵犯版权请通知本人整改。


上一篇: Cocos3.4 横版游戏制作-《KillBear》-边缘检测 地图滚动
上一篇中,我们实现了边缘检测+地图滚动 .
本篇中,我们在OperateLayer.cpp加入一个攻击按钮,实现Hero攻击
并在左上角加入一个血条显示


开发环境

win64 : vs2010
Cocos2d-x v3.4Final
TexturePackerGUI
MapEdit


代码构建A

Operate

OperateLayer

  • .h

加入

void attackButton(Ref* pSender);
  • .cpp

init中

auto visibleSize = Director::geTinstance()->getVisibleSize();
        //创造attackItem图标
        auto attackItem = MenuItemImage::create("attackbuttonNormal.png","attackbuttonSELEcted.png",CC_CALLBACK_1(OperateLayer::attackButton,this));

        //图标大小
        attackItem->setScale(1.5);
        //attackItem->setContentSize(Size(50,50));

        //图标放在
        attackItem->setPosition(visibleSize.width - attackItem->getContentSize().width/2-50,attackItem->getContentSize().height/2+20);

        auto menu = Menu::create(attackItem,NULL);
        menu->setPosition(Vec2::ZERO);
        this->addChild(menu,100);
void OperateLayer::attackButton(Ref* pSender)
{
    global->hero->onAttack(1);
}

效果A


之后在左上角放一个血条显示,将其添加进StateLayer


代码构建

State

StateLayer

  • .h
#include "HpShow.h"
  • .cpp

init

auto Hp = HpShow::create();
        this->addChild(Hp);

新建一个HpShow

HpShow

  • .h
#ifndef _HP_SHOW_H_
#define _HP_SHOW_H_
#include "cocos2d.h"
#include "Global.h"
USING_NS_Cc;

class HpShow : public Node
{
public:
    HpShow();
    ~HpShow();
    virtual bool init();
    void update(float dt);
    CREATE_FUNC(HpShow);
private:
    ProgressTimer* m_preal;
};

#endif
  • .cpp
#include "HpShow.h"

HpShow::HpShow()
{
}
HpShow::~HpShow()
{
}
bool HpShow::init()
{
    bool ret = false;
    do {
        CC_BREAK_IF( !Node::init());
        auto visibleSize =  Director::geTinstance()->getVisibleSize();

        //创建血条背景
        Sprite* HeroHp = Sprite::create("bloodBg.png");
        HeroHp->setPosition(HeroHp->getContentSize().width/2,visibleSize.height-HeroHp->getContentSize().height/2);
        this->addChild(HeroHp);

        //一个ProgressTime类,能够设置其状态,百分比,显示类型等
        m_preal = ProgressTimer::create(Sprite::create("blood.png"));
        m_preal->setType(ProgressTimer::Type::BAR);
        m_preal->setMidpoint(Point(0,0.5));     
        m_preal->setBarChangeRate(Point(1.0,0));   
        m_preal->setPosition(HeroHp->getContentSize()/2);
        m_preal->setPercentage(100);
        HeroHp->addChild(m_preal);

        this->schedule@R_616_4457@;
        ret = true;
    } while(0);
    return ret;
}


void HpShow::update(float dt)
{
    //更新下血条状态
    m_preal->setPercentage((float)global->hero->getCurtLifeValue()/global->hero->getSumLifeValue()*100);
}

结果B

结语

本篇添加了一个血条和攻击按键.使得我么的Hero可以播放攻击动画 每一帧都会更新血量,刷新血条状态,显示当前血量 下一篇实现敌人的加入,并为其添加一些简单的AI.

大佬总结

以上是大佬教程为你收集整理的Cocos3.4 横版游戏制作-《KillBear》-添加血条 攻击按键全部内容,希望文章能够帮你解决Cocos3.4 横版游戏制作-《KillBear》-添加血条 攻击按键所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。