功夫小子实践开发-英雄实体类的基本分析和实现

*图片来源见水印

进行的基本步骤是:
  • •确定主角所具有的属性
  • •确定主角所具有的方法
  • •编码实现主角英雄类
首先是英雄的一些基本属性,包括

而具有的方法则分为:
代码如下:

.h文件

#ifndef Hero_H__
#define Hero_H__

#include "cocos2d.h"
USING_NS_CC;

class Hero : public Node//直接继承自Sprite也行
{
public:
	//属性
	bool IsDead;

	//HP & MP 值
	float m_iCurrentHp;		//当前血量
	float m_iTotleHp;		//总血量
	float m_iCurrentMp;		//当前能量
	float m_iTotleMp;		//总能量
	float percentage;		//当前血量比

	float m_iSpeed;			//移动速度
	bool m_bIsAction;		//是否已经开始打怪了
	bool m_bIsJumping;		//跳跃标志
	bool IsRunning;
	bool IsAttack;
	bool IsHurt;
	bool HeroDirection;		//英雄运动的方向
	bool m_bCanCrazy;		//狂暴标志

	//构造性方法
	//根据图片名创建英雄
	void InitHeroSprite(char *hero_name, int iLevel);
	//返回当前英雄
	Sprite* GetSprite();
	CREATE_FUNC(Hero);

	//动作方法
	//设置动画,run_dirction为精灵朝向,false朝右,frameName为图片帧名字
	void SetAnimation(const char *frameName, float delay, bool run_direction);
	
	void StopAnimation();

	void JumpUpAnimation(const char *name_each, float delay, bool run_direction);

	void JumpDownAnimation(const char *name_each, float delay, bool run_direction);

	void JumpEnd();

	void AttackAnimation(const char *name_each, float delay, bool run_direction);

	void AttackEnd();

	void DeadAnimation(const char *name_each, float delay, bool run_direction);

	void DeadEnd();

	void HurtByMonsterAnimation(const char *name_each, float delay, bool run_direction);

	void HurtByMonsterEnd();

	bool JudgePosition(Size visibleSize);

private:
	Sprite* m_HeroSprite;//精灵
	char *Hero_name;	//用来保存初始状态的精灵图片名称,每次完成动作之后都要回到这个初始不动的状态
};

#endif

.cpp文件

#include "Hero.h"
#include "ActionRool.h"

USING_NS_CC;

//构造性方法
void Hero::InitHeroSprite(char *hero_name, int m_iLevel)
{
	//属性初始化
	m_HeroSprite = NULL;
	m_bCanCrazy = false;
	m_bIsAction = false;
	m_bIsJumping = false;
	HeroDirection = false;	//向右运动
	Hero_name = NULL;
	IsRunning = false;
	IsAttack = false;
	IsHurt = false;
	IsDead = false;
	m_iCurrentMp = 0.0f;
	m_iTotleMp = 100.0f;
	m_iSpeed = 5;
	Hero_name = hero_name;
	m_iCurrentHp = m_iTotleHp = 300.0f*m_iLevel;
	percentage = 100.0f;

	this->m_HeroSprite = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(hero_name));
	this->addChild(m_HeroSprite);
}

Sprite* Hero::GetSprite()
{
	return m_HeroSprite;
}

//动作方法
//设置动作
void Hero::SetAnimation(const char *frameName, float delay, bool run_direction)
{
	//调整方向
	if(HeroDirection !=run_direction)
	{
		HeroDirection = run_direction;
		m_HeroSprite->setFlippedX(run_direction);
	}
	if(IsRunning || IsHurt || IsAttack)
		return;

	//创建动画动作
	Animate* action = ActionTool::animationWithFrameName(frameName,-1,delay);
	m_HeroSprite->runAction(action);
	IsRunning = true;
}

void Hero::StopAnimation()
{
	if(!IsRunning)
		return;
	m_HeroSprite->stopAllActions();//当前精灵停止所有动画

	//恢复精灵原来的初始化贴图
	this->removeChild(m_HeroSprite,true);//把原来的精灵删除掉
	this->m_HeroSprite = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(Hero_name));
	m_HeroSprite->setFlippedX(HeroDirection);
	this->addChild(m_HeroSprite);
	IsRunning = false;
}

//跳起
void Hero::JumpUpAnimation(const char *name_each, float delay, bool run_direction)
{
	//调整方向
	if(HeroDirection != run_direction)
	{
		HeroDirection = run_direction;
		m_HeroSprite->setFlippedX(run_direction);
	}
	if(IsHurt || IsAttack || IsDead)
		return;

	//创建动画动作
	Animate* action = ActionTool::animationWithFrameName(name_each,-1,delay);
	m_HeroSprite->runAction(action);
	m_bIsJumping = true;
}

//跳落
void Hero::JumpDownAnimation(const char *name_each, float delay, bool run_direction)
{
	//调整方向
	if(HeroDirection != run_direction)
	{
		HeroDirection = run_direction;
		m_HeroSprite->setFlippedX(run_direction);
	}
	if(IsHurt || IsAttack)
		return;

	Animate* action = ActionTool::animationWithFrameName(name_each,-1,delay);
	m_HeroSprite->runAction(action);
	m_bIsJumping = true;
}

//跳完
void Hero::JumpEnd()
{
	m_HeroSprite->stopAllActions();
	//恢复精灵原来的初始化贴图
	this->removeChild(m_HeroSprite,true);//把原来的精灵删除掉
	this->m_HeroSprite = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(Hero_name));
	m_HeroSprite->setFlippedX(HeroDirection);
	this->addChild(m_HeroSprite);
	m_bIsJumping = false;
}

//攻击
void Hero::AttackAnimation(const char *name_each, float delay, bool run_direction)
{
	//调整方向
	if(HeroDirection != run_direction)
	{
		HeroDirection = run_direction;
		m_HeroSprite->setFlippedX(run_direction);
	}
	if(IsAttack || m_bIsJumping)
		return;

	//创建动画动作
	Animate* action = ActionTool::animationWithFrameName(name_each, 1, delay);  
    //创建回调动作,受伤动画结束调用HurtEnd()  
	CallFunc* callFunc = CallFunc::create(this, callfunc_selector(Hero::AttackEnd));  
    //创建连续动作  
    ActionInterval* attackact = Sequence::create(action, callFunc, NULL);  
    m_HeroSprite->runAction(attackact);  
    IsAttack = true;  
}

void Hero::AttackEnd()
{
	m_HeroSprite->setFlippedX(HeroDirection);
	IsAttack = false;
	if(m_bCanCrazy == true)
	{
		m_bCanCrazy = false;
		m_iCurrentMp = 0;
	}
}

// 受伤  
void Hero::HurtByMonsterAnimation(const char *name_each, float delay, bool run_directon)  
{  
    if (IsHurt || IsDead)  
        return;  
    //受伤优先  
    if (IsRunning || IsAttack)  
    {  
        m_HeroSprite->stopAllActions();//当前精灵停止所有动画  
        //恢复精灵原来的初始化贴图   
        this->removeChild(m_HeroSprite, true);//把原来的精灵删除掉  
        m_HeroSprite = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(Hero_name));//恢复精灵原来的贴图样子  
        m_HeroSprite->setFlippedX(HeroDirection);  
        this->addChild(m_HeroSprite);  
        IsRunning = false;  
        IsAttack = false;  
    }  
  
    Animate* action = ActionTool::animationWithFrameName(name_each, 1, delay);  
    //创建回调动作,受伤动画结束调用HurtEnd()  
    CallFunc* callFunc = CallFunc::create(this, callfunc_selector(Hero::HurtByMonsterEnd));  
    //创建连续动作  
    ActionInterval* hurtackact = Sequence::create(action, callFunc, NULL);  
  
    m_HeroSprite->runAction(hurtackact);  
    IsHurt = true;  
  
  
}  
  
// 受伤结束  
void Hero::HurtByMonsterEnd()  
{  
    m_iCurrentHp -= 20.0f;  
    IsHurt = false;  
    percentage = m_iCurrentHp / m_iTotleHp * 100.0f;  
    if (m_iCurrentHp < 0.0f)  
    {  
        DeadAnimation("dead", 0, HeroDirection);  
    }  
}  
  
// 死亡  
void Hero::DeadAnimation(const char *name_each, float delay, bool run_directon)  
{  
    m_HeroSprite->stopAllActions();  
    // 调整方向  
    if (HeroDirection != run_directon)  
    {  
        HeroDirection = run_directon;  
        m_HeroSprite->setFlippedX(run_directon);  
    }  
    // 创建动作  
    Animate* act = ActionTool::animationWithFrameName(name_each, 1, delay);  
    //创建回调动作,攻击结束后调用AttackEnd()  
    CallFunc* callFunc = CallFunc::create(this, callfunc_selector(Hero::DeadEnd));  
    //创建连续动作  
    ActionInterval* attackact = Sequence::create(act, callFunc, NULL);  
    m_HeroSprite->runAction(attackact);  
    Director::getInstance()->getScheduler()->setTimeScale(0.5);  
}  
  
// 死亡结束  
void Hero::DeadEnd()  
{  
    IsDead = true;  
    //恢复死亡的样子  
    this->removeChild(m_HeroSprite, true);  //把原来的精灵删除掉  
    m_HeroSprite = Sprite::createWithSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName("monsterDie6.png")); //恢复死亡的样子  
    m_HeroSprite->setFlippedX(HeroDirection);  
    this->addChild(m_HeroSprite);  
  
}  
  
// 判断位置  
bool Hero::JudgePosition(Size visibleSize)  
{  
    if (this->getPositionX() > (visibleSize.width / 2.0 + 2.0) || (this->getPositionX() < visibleSize.width / 2.0 - 2.0)) // 精灵到达mid?  
        return false;  
    else  
        return true;//到达中间位置  
}  


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值