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

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

}

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

相关文章

    本文实践自 RayWenderlich、Ali Hafizji 的文章《...
Cocos-code-ide使用入门学习地点:杭州滨江邮箱:appdevzw@1...
第一次開始用手游引擎挺激动!!!进入正题。下载资源1:从C...
    Cocos2d-x是一款强大的基于OpenGLES的跨平台游戏开发...
1.  来源 QuickV3sample项目中的2048样例游戏,以及最近《...
   Cocos2d-x3.x已经支持使用CMake来进行构建了,这里尝试...