展会信息港展会大全

cocos2d中分步实现飞机大战----游戏场景中背景的滚动
来源:互联网   发布日期:2015-09-28 10:51:52   浏览:1161次  

导读: 上一节说了场景的跳转,现在开始布置游戏游戏界面。在游戏的主界面,首先要有游戏背景,为了使GameScene的代码不至于太多,可以吧自己的背景进行封装,在GameScene中调用就好,飞机的正常...

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

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;

}

赞助本站

人工智能实验室
AiLab云推荐
推荐内容
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港