上一节说了场景的跳转,现在开始布置游戏游戏界面。在游戏的主界面,首先要有游戏背景,为了使GameScene的代码不至于太多,可以吧自己的背景进行封装,在GameScene中调用就好,飞机的正常飞行移动可以用北京的移动来实现。创建BackGround:
background.h:
#include "cocos2d.h"
USING_NS_CC;
class background:public Node{
public:
CREATE_FUNC(background);
bool init();
void moveBg(float t);
};
Background.cpp:
#include "background.h"
bool background::init(){
if (!Node::init()) {
return false;
}
auto sp= Sprite::create("back0.png");
this->addChild(sp);
sp->setTag(111);
sp->setAnchorPoint(Vec2(0,0));
sp->cocos2d::Node::setPosition(0,0);
auto sp2= Sprite::create("back0.png");
this->addChild(sp2);
sp2->setTag(222);
sp2->setAnchorPoint(Vec2(0,0));
sp2->setPosition(0,sp->getContentSize().height);
this->schedule(schedule_selector(background::moveBg),1/60.0f); //-----计划任务每1/60秒调用一次moveBg
return true;
}
void background::moveBg(float t){ //-------背景移动--滚屏的实现
auto sp=this->getChildByTag(111);
auto sp2=this->getChildByTag(222);
sp->setPositionY(sp->getPositionY()-1);
if (sp->getPositionY()<=-1136) {
sp->setPositionY(0);
}
sp2->setPositionY(sp->getPositionY()+sp->getContentSize().height);
}
在gameScene中调用:
#include "gameScene.h"
#include "background.h"
Scene * gameScene::createScene(){
auto sc=Scene::create();
gameScene * ms=gameScene::create();
sc->addChild(ms);
return sc;
}
bool gameScene::init(){
if (!Layer::init()) {
return false;
}
background * bg=backgroundcreate(); //---实例化
this->addChild(bg);
return true;
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。