cocos2d中分步实现飞机大战----游戏场景中背景的滚动
上一节说了场景的跳转,现在开始布置游戏游戏界面。在游戏的主界面,首先要有游戏背景,为了使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; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |