zoukankan      html  css  js  c++  java
  • 创建敌人基类

    class EnemyBase : public Sprite  
    {  
    public:      
        virtual bool init() override;  
        CREATE_FUNC(EnemyBase);  
        Animation* createAnimation(std::string prefixName, int framesNum, float delay);  
        void changeDirection(float dt);  
        Node* currPoint();  
        Node* nextPoint();  
        void runFllowPoint();  
        void setPointsVector(Vector<Node*> points);      
    private:  
        Vector<Node*> pointsVector;  
    protected:  
        int pointCounter;  
        Animation *animationRight;  
        Animation *animationLeft;  
        CC_SYNTHESIZE(float, runSpeed, RunSpeed);      
    };
    //实现
    Node* EnemyBase::currPoint()  
    {  
        return this->pointsVector.at(pointCounter);  
    }  
    Node* EnemyBase::nextPoint()  
    {  
        int maxCount = this->pointsVector.size();  
    pointCounter++;  
    if (pointCounter < maxCount  ){  
    auto node =this->pointsVector.at(pointCounter);  
            return node;  
        }  
        else{  
            pointCounter = maxCount -1 ;  
        }  
        return NULL;  
    }
    //小偷是敌人要继承敌人基类
    class Thief : public EnemyBase  
    {  
    public:  
        virtual bool init() override;      
        static Thief* createThief(Vector<Node*> points);  
    };
    //在基类已经给出了敌人的各种逻辑方法,所以在Thief中,我们只需要初始化变量,实现具体的方法,就可以实现一个很普通的敌人了。   
    bool Thief::init()  
    {  
    if (!Sprite::init())  
    {  
    return false;  
    }  
    // 1  
        setRunSpeed(6);  
        animationRight = createAnimation("enemyRight1", 4, 0.1f);  
    AnimationCache::getInstance()->addAnimation(animationRight, "runright");  
        animationLeft = createAnimation("enemyLeft1", 4, 0.1f);  
    AnimationCache::getInstance()->addAnimation(animationLeft, "runleft");  
    // 2  
        schedule(schedule_selector(EnemyBase::changeDirection), 0.4f);  
    return true;  
    }
    //创建小偷的接口函数
    Thief* Thief::createThief(Vector<Node*> points)  
    {  
        Thief *pRet = new Thief();  
        if (pRet && pRet->init())  
        {  
         // 设置小偷的路径点集  
            pRet->setPointsVector(points);  
            // 让小偷沿着路径点移动  
            pRet->runFllowPoint();  
            pRet->autorelease();  
            return pRet;  
        }  
        else  
        {  
            delete pRet;  
            pRet = NULL;  
            return NULL;  
        }  
    }
  • 相关阅读:
    JavaScript DOM编程艺术 读书笔记(简略)
    关于暂停或终止更新的相关读书笔记
    Core Java Volume II—Using annotations
    Data Structure and Algorithms Analysis in C Note (II)
    Hibernate实战——理解对象/关系持久化 笔记
    Data Structure and Algorithms Analysis in C Note (I)
    Java 8实战 第三章
    GitHub入门与实践 学习笔记(二)
    Computer Networking A Top-Down Approach 笔记(一)
    进程基础
  • 原文地址:https://www.cnblogs.com/newlist/p/4127273.html
Copyright © 2011-2022 走看看