上一节说了背景的滚动,现在开始布置游戏中自己的飞机,为了使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;
}
这样在游戏场景中就出现自己的飞机了。
原文地址:https://www.jb51.cc/cocos2dx/344092.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。