[cocos2d-x][游戏开发]通过cocos2d-x实现简易飞机大战 09.游戏界面 道具

来源:互联网 发布:游戏源码怎么用 编辑:程序博客网 时间:2024/04/29 04:53

道具的产生就是在飞机碰撞检测之后根据设定好的概率,产生一个带有各种效果的图层,

class Tool:public Node{    public :    Sprite * sp;    CREATE_FUNC(Tool);    bool init();    static Tool * newTool(int type,int x,int y);    int tx,ty;    int type;    int times;    bool dirH,dirV;    void moveTo(int x,int y);    void update(float t);};

单据产生之后还需要移动以及道具种类
Tool * Tool::newTool(int type,int x,int y){    Tool * nt=Tool::create();    switch(type)    {        case 1:            nt->sp=Sprite::create("toolh.png");            break;        case 2:            nt->sp=Sprite::create("toola.png");            break;        case 3:            nt->sp=Sprite::create("tooll.png");            break;          }    nt->type=type;    nt->addChild(nt->sp);    nt->moveTo(x, y);    return nt;}void Tool::moveTo(int x,int y){    this->sp->setPosition(Vec2(x,y));    this->tx=x;    this->ty=y;}bool Tool::init(){    if(!Node::init())    {return false;}    //计划任务 自己移动    dirH=true;    dirV=true;    times=0;    this->scheduleUpdate();    return true;}void Tool::update(float t){ //移动当前道具    if(dirH)    {        this->moveTo(tx+5,ty);    }else    { this->moveTo(tx-5,ty);            }    if(dirV)    {        this->moveTo(tx,ty+5);    }else    { this->moveTo(tx,ty-5);            }    if(tx<0||tx>Director::getInstance()->getWinSize().width)    {        dirH=!dirH;        times++;    }    if(ty<0||ty>Director::getInstance()->getWinSize().height)    {        dirV=!dirV;        times++;    }    if(times>4)    {        this->removeFromParentAndCleanup(true);    }}
最后那 飞机与道具进行碰撞检测,获得该道具

0 0
原创粉丝点击