加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Cocos2d-x背景的循环滚动

发布时间:2020-12-14 19:46:47 所属栏目:百科 来源:网络整理
导读://.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 coco
//.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
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读