展会信息港展会大全

cocos2d中分步实现飞机大战----自己飞机的实现
来源:互联网   发布日期:2015-09-28 10:51:53   浏览:1555次  

导读: 上一节说了背景的滚动,现在开始布置游戏中自己的飞机,为了使GameScene的代码不至于太多,可以吧自己的飞机进行封装,在GameScene中调用就好。创建Plane: Plane.h:...

上一节说了背景的滚动,现在开始布置游戏中自己的飞机,为了使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;

}

这样在游戏场景中就出现自己的飞机了。

赞助本站

人工智能实验室
AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港