newlist

  博客园 :: 首页 :: 博问 :: 闪存 :: 新随笔 :: 联系 :: 订阅 订阅 :: 管理 ::
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;  
    }  
}

 

posted on 2014-11-27 21:51  一枚程序  阅读(257)  评论(0编辑  收藏  举报