道具的产生就是在飞机碰撞检测之后根据设定好的概率,产生一个带有各种效果的图层,
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);
}
}
最后那 飞机与道具进行碰撞检测,获得该道具