道具的产生就是在飞机碰撞检测之后根据设定好的概率,产生一个带有各种效果的图层,
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);
}
}最后那 飞机与道具进行碰撞检测,获得该道具
本文介绍了一种在游戏中实现道具的方法,并详细展示了如何通过代码创建不同类型的道具,以及这些道具如何在游戏环境中移动并最终与玩家飞机进行碰撞检测。文章还提供了一个具体的道具类实现示例。

3797

被折叠的 条评论
为什么被折叠?



