cocos2d中分步实现飞机大战----游戏场景中背景的滚动

cocos2d中分步实现飞机大战----游戏场景中背景的滚动,第1张

概述上一节说了场景的跳转,现在开始布置游戏游戏界面。在游戏的主界面,首先要有游戏背景,为了使GameScene的代码不至于太多,可以吧自己的背景进行封装,在GameScene中调用就好,飞机的正常飞行移动可以用北京的移动来实现。创建BackGround: background.h: #include "cocos2d.h" USING_NS_CC; class background:public No

上一节说了场景的跳转,现在开始布置游戏游戏界面。在游戏的主界面,首先要有游戏背景,为了使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;

}

总结

以上是内存溢出为你收集整理的cocos2d中分步实现飞机大战----游戏场景中背景的滚动全部内容,希望文章能够帮你解决cocos2d中分步实现飞机大战----游戏场景中背景的滚动所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1046899.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-24
下一篇 2022-05-24

发表评论

登录后才能评论

评论列表(0条)

保存