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

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

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值