您的位置:首页 > 移动开发 > Cocos引擎

cocos2d中分步实现飞机大战----自己飞机的实现

2015-03-08 23:52 225 查看
上一节说了背景的滚动,现在开始布置游戏中自己的飞机,为了使GameScene的代码不至于太多,可以吧自己的飞机进行封装,在GameScene中调用就好。创建Plane:

Plane.h:

#include "cocos2d.h"

USING_NS_CC;

class plane:public
Node{

public:

int hp=100;

int px,py;

CREATE_FUNC(plane);

bool init();

void moveTo(int x,int y);
};
Plane.cpp:

#include "plane.h"
bool
plane::init(){

if (!Node::init()) {

return
false;
}

auto sp=Sprite::create("boss0.png");
sp->setTag(333);

this->addChild(sp);

return
true;
}
void
plane::moveTo(int x,
int y){

this->getChildByTag(333)->setPosition(x,
y);

this->px=x;

this->py=y;
}
在GameScene中调用
首先在开始引用Plane.h
#include "Plane.h"
在init()中实例化:

bool
gameScene::init(){

if (!Layer::init()) {

return
false;
}

plane * p=plane::create();

this->addChild(p);

p->moveTo(Director::getInstance()->getWinSize().width/2,
200);
p->setTag(444);

return
true;
}
这样在游戏场景中就出现自己的飞机了。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐