前端之家收集整理的这篇文章主要介绍了
Cocos2d-x背景的循环滚动,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//.h文件
#ifndef __HELLOWORLD_SCENE_H__
#define __HELLOWORLD_SCENE_H__
#include "cocos2d.h"
USING_NS_CC;
class HelloWorld : public cocos2d::Layer
{
public:
// there's no 'id' in cpp,so we recommend returning the class instance pointer
static cocos2d::Scene* createScene();
// Here's a difference. Method 'init' in cocos2d-x returns bool,instead of returning 'id' in cocos2d-iphone
virtual bool init();
// a selector callback
void menuCloseCallback(cocos2d::Ref* pSender);
void makebg(float num,int w,int tag);
void movedbg(float times);
void getScreen(float times);
// implement the "static create()" method manually
CREATE_FUNC(HelloWorld);
private:
Size visibleSize;
Vec2 origin;
Node* node1;
Node* node2;
Sprite* map;
std::string path;
};
#endif // __HELLOWORLD_SCENE_H__
//.cpp
#include "HelloWorldScene.h"
USING_NS_CC;
Scene* HelloWorld::createScene()
{
// 'scene' is an autorelease object
auto scene = Scene::create();
// 'layer' is an autorelease object
auto layer = HelloWorld::create();
// add layer as a child to scene
scene->addChild(layer);
// return the scene
return scene;
}
// on "init" you need to initialize your instance
bool HelloWorld::init()
{
//////////////////////////////
// 1. super init first
if ( !Layer::init() )
{
return false;
}
visibleSize = Director::getInstance()->getVisibleSize();
origin = Director::getInstance()->getVisibleOrigin();
/////////////////////////////
// 2. add a menu item with "X" image,which is clicked to quit the program
// you may modify it.
// add a "close" icon to exit the progress. it's an autorelease object
auto closeItem = MenuItemImage::create(
"CloseNormal.png","CloseSelected.png",CC_CALLBACK_1(HelloWorld::menuCloseCallback,this));
closeItem->setPosition(Vec2(origin.x + visibleSize.width - closeItem->getContentSize().width/2,origin.y + closeItem->getContentSize().height/2));
// create menu,it's an autorelease object
auto menu = Menu::create(closeItem,NULL);
menu->setPosition(Vec2::ZERO);
this->addChild(menu,1);
auto bg1 = Sprite::create("bg.png");
bg1->ignoreAnchorPointForPosition(false);
bg1->setAnchorPoint(Vec2::ZERO);
bg1->setTag(1);
bg1->setPosition(Vec2::ZERO);
node1 = Node::create();
node1->setPosition(Vec2(0,0));
addChild(node1);
node1->addChild(bg1);
auto bg2 = Sprite::create("bg.png");
bg2->ignoreAnchorPointForPosition(false);
bg2->setAnchorPoint(Vec2::ZERO);
bg2->setTag(2);
bg2->setPosition(Vec2::ZERO);
node2 = Node::create();
node2->setPosition(Vec2(bg2->getContentSize().width,0));
addChild(node2);
node2->addChild(bg2);
path = FileUtils::getInstance()->getWritablePath() + "smallTield.png";
map = Sprite::create();
map->setScale(0.25);
map->ignoreAnchorPointForPosition(false);
map->setAnchorPoint(Vec2::ANCHOR_MIDDLE_TOP);
map->setPosition(Vec2(visibleSize.width/2,visibleSize.height));
addChild(map,2);
schedule(schedule_selector(HelloWorld::movedbg));
return true;
}
void HelloWorld::getScreen(float times)
{
}
void HelloWorld::movedbg(float times)
{
auto back1 = node1->getChildByTag(1);
auto back2 = node2->getChildByTag(2);
//每帧移动的距离,可根据个人爱好更改
node1->setPosition(Vec2(node1->getPositionX() - 15,node1->getPositionY()));
node2->setPosition(Vec2(node2->getPositionX() - 15,node2->getPositionY()));
if(node1->getPositionX() <= (-back1->getContentSize().width))
{
node1->setPosition(Vec2(node2->getPositionX() + back1->getContentSize().width,node1->getPositionY()));
}
if(node2->getPositionX() <= (-back2->getContentSize().width))
{
node2->setPosition(Vec2(node1->getPositionX() + back2->getContentSize().width,node2->getPositionY()));
}
}
void HelloWorld::menuCloseCallback(Ref* pSender)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WP8) || (CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
MessageBox("You pressed the close button. Windows Store Apps do not implement a close button.","Alert");
return;
#endif
Director::getInstance()->end();
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
exit(0);
#endif
}