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=background::create();   //---实例化

    this->addChild(bg);

    return true;

}




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值