精灵在TiledMap中运动

本人也是刚刚开始记录,在编程cocos2d-x代码中的点点滴滴,有什么写的不对,请各位大神多多指教啊。
主要的功能:我先通过TiledMap在地图上通过Object对象,获取这些点组成的一个路径。在通过读取TiledMap地图中的Object对象,将这些对象放入一个数组,在本类中将TiledMap中的对象传入本类的初始化的方法中。
 .h中只要是声明一些属性和方法,具体的实现我们会在cpp文件中细说。
BaseEnemy.h 文件
typedef  enum
{
    MoveNone,  //空
    MoveDICUP,   //向上
    MoveDICDOWN,  //向下
    MoveDICLEFT,   //向左
    MoveDICRIGHT,  //向右
}Direction;
#include <iostream>
#include <cocos2d.h>
class BaseEnemy:public cocos2d::CCSprite
{
public:
    //初始化
    virtual bool init(const char * fileName,std::vector<cocos2d::CCPoint> vect);
    //怪物的移动
    void moves();
    //改变方向的方法
    Direction exchangeDic(cocos2d::CCPoint p);
    //运动帧动画
    cocos2d::CCAnimate* createAnimate(const char* fileName,int counts,int loops);
    //旋转方法
    void rotation(Direction dic);
    
    void movCallFunc();
private:
    bool isDie;  //是否死亡
    int hp;  //血量
    Direction dic; //方向
    int speed; //速度
    int attcak;  //怪物的攻击力
    cocos2d::CCPoint currentP; //初始点
    std::vector<cocos2d::CCPoint> monsterPoint;  //存取点的数组
    int index;  //下标


cpp文件我会在代码中进行细说。

BaseEnemy.cpp 文件
<pre name="code" class="cpp">#include "BaseEnemy.h"
using namespace cocos2d;
//由于我继承的是CCSprite,所以我重写了init()方法
bool BaseEnemy::init(const char * fileName,std::vector<cocos2d::CCPoint> vect)
{
    //initWithSpriteFrameName()此方法在使用的时候,一定要注意,一定要先加载plist文件,才能使用, 
    if(!CCSprite::initWithSpriteFrameName(fileName))
    {
        return false;
    }
    //初始化数据
    monsterPoint = vect;
    this->isDie = false;
    this->hp = 0;
    this->attcak = 0;
    this->dic = MoveNone;
    this->index = 1;
    this->moves();
    return true;
}


void BaseEnemy::movCallFunc()
{
    currentP =monsterPoint.at(index);
    CCLog("1---1-%f,%f",currentP.x,currentP.y);
    index++;
    CCLog("index --%d",index);
    if (index < monsterPoint.size())
    {
        this->moves();
    }
}
void BaseEnemy::moves()
{
    currentP = monsterPoint.at(index-1);
    CCLog("1---1-%f,%f",currentP.x,currentP.y);
    CCPoint a =  currentP ;
    CCPoint nextP = monsterPoint.at(index);
    CCLog("2---2-%f,%f",nextP.x,nextP.y);
    Direction dir = exchangeDic(nextP);
    double distance =sqrt((nextP.x-a.x)*(nextP.x-a.x)+(nextP.y-a.y)*(nextP.y-a.y)) ;
    switch (dir) {
        case MoveDICUP:
        {
            CCMoveTo* moveUp = CCMoveTo::create(3, nextP);
            auto call = CCCallFunc::create(this, callfunc_selector(BaseEnemy::movCallFunc));
            this->runAction(CCSequence::create(moveUp,call,NULL));
            break;
        }
        case MoveDICDOWN:
        {
            CCMoveTo* moveDown = CCMoveTo::create(3, nextP);
            //CCCallFunc* callfun = CCCallFunc::create(this, callfunc_selector(BaseEnemy::rotation));
            auto call = CCCallFunc::create(this, callfunc_selector(BaseEnemy::movCallFunc));
            this->runAction(CCSequence::create(CCSpawn::create(moveDown,NULL),call,NULL));
            break;
        }
        case MoveDICLEFT:
        {
            CCMoveTo* moveLeft = CCMoveTo::create(3, nextP);
            auto call = CCCallFunc::create(this, callfunc_selector(BaseEnemy::movCallFunc));
            this->runAction(CCSequence::create(moveLeft,call,NULL));
            break;
        }
        case MoveDICRIGHT:
        {
            CCMoveTo* moveRight = CCMoveTo::create(3, nextP);
            auto call = CCCallFunc::create(this, callfunc_selector(BaseEnemy::movCallFunc));
            this->runAction(CCSequence::create(moveRight,call,NULL));
            break;
        }
        default:
            break;
    }

}
//改变方向的方法
Direction BaseEnemy::exchangeDic(cocos2d::CCPoint p)
{
    //判断怪物移动的方向  向上
    if(currentP.x == p.x && currentP.y<p.y)
    {
         this->dic = MoveDICUP;
    }else if (currentP.y == p.y && currentP.x<p.x){  //向右
         this->dic = MoveDICRIGHT;
    }else if (currentP.x == p.x && currentP.y>p.y){  //向下
         this->dic = MoveDICDOWN;
    }else if (currentP.y == p.y && currentP.y>p.y){  //向左
         this->dic = MoveDICLEFT;
    }
    
    return this->dic;
}
//帧动画
CCAnimate* BaseEnemy::createAnimate(const char* fileName,int counts,int loops)
{
    CCAnimation* ani = CCAnimation::create();
    for(int i = 0;i < counts; i++)
    {
        //2.x的版本使用下的,如果是3.x要是std::string f = StringUtils::Format(fileName,i)
        CCString * str = CCString::createWithFormat(fileName,i);
        //从缓存中读取信息
        CCSpriteFrame * sf =CCSpriteFrameCache::sharedSpriteFrameCache()
                                            ->spriteFrameByName(str->getCString());
        //添加
        ani->addSpriteFrame(sf);
    }
    ani->setDelayPerUnit(0.1);
    ani->setLoops(loops);
    return CCAnimate::create(ani);
}

//旋转方法,在使用旋转,缩放等动画的的时候一定要注意,因为他们都是根据锚点来进行转变的
void BaseEnemy::rotation(Direction dic)
{
    if(dic == MoveDICDOWN ||dic == MoveDICLEFT)
    {
        this->setRotationY(180);
    }
}


 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值