zoukankan      html  css  js  c++  java
  • 【cocos2d-x 3.7 飞机大战】 决战南海I (八) 背景移动

            採用双层背景。这样效果更好


    .h

    class BackgroundMove : public Layer
    {
    public:
    	BackgroundMove();
    	~BackgroundMove();
    	virtual bool init();
    	virtual void onEnterTransitionDidFinish();	//等进入场景之后在进行背景的移动
    	CREATE_FUNC(BackgroundMove);
    
    public:
    	void move(float dt);
    private:
    	Sprite* m_background1;
    	Sprite* m_background2;
    	Sprite* m_background3;
    	Sprite* m_background4;
    
    	enum 
    	{
    		OFFSET = 3
    	};
    };


    背景无限滚动的方式有非常多,仅仅要不出现黑边就可以

    .cpp

    BackgroundMove::BackgroundMove() : m_background1(NULL), m_background2(NULL), m_background3(NULL), m_background4(NULL)
    {
    
    }
    BackgroundMove::~BackgroundMove()
    {
    	CC_SAFE_DELETE(m_background1);
    	CC_SAFE_DELETE(m_background2);
    	CC_SAFE_DELETE(m_background3);
    	CC_SAFE_DELETE(m_background4);
    }
    bool BackgroundMove::init()
    {
    	bool bRect = false;
    
    	do 
    	{
    		if (!Layer::init())
    			return false;
    
    		//载入背景图片
    		m_background1 = Sprite::createWithSpriteFrameName("backgroundTollgate2.png");
    		m_background1->setPosition(Point(0,0));
    		m_background1->setAnchorPoint(Vec2(0, 0));
    
    		this->addChild(m_background1,1);
    
    		m_background2 = Sprite::createWithSpriteFrameName("backgroundTollgate2.png");
    		m_background2->setPosition(Point(0, 0));
    		m_background2->setAnchorPoint(Vec2(0, 0));
    		m_background2->setFlipY(true);
    
    		this->addChild(m_background2,1);
    
    		//载入背景图片
    		m_background3 = Sprite::createWithSpriteFrameName("backgroundTollgateThree.png");
    		m_background3->setPosition(Point(0, 0));
    		m_background3->setAnchorPoint(Vec2(0, 0));
    
    		this->addChild(m_background3, 0);
    
    		m_background4 = Sprite::createWithSpriteFrameName("backgroundTollgateThree.png");
    		m_background4->setPosition(Point(0, 0));
    		m_background4->setAnchorPoint(Vec2(0, 0));
    		m_background4->setFlipY(true);
    
    		this->addChild(m_background4, 0);
    
    		CocosDenshion::SimpleAudioEngine::getInstance()->playBackgroundMusic("sound/BackgroundMusic.mp3", true);
    
    		bRect = true;
    	} while (0);
    
    	return bRect;
    }
    void BackgroundMove::onEnterTransitionDidFinish()
    {
    	Layer::onEnterTransitionDidFinish();
    	this->schedule(SEL_SCHEDULE(&BackgroundMove::move), 0.01f);
    }
    
    void BackgroundMove::move(float dt)
    {
    	Vec2 origin = Director::getInstance()->getVisibleOrigin();
    
    	m_background1->setPositionY(m_background1->getPositionY() - OFFSET);
    	m_background2->setPositionY(m_background1->getPositionY() + m_background1->getContentSize().height);
    	if (m_background2->getPositionY() <= origin.y)
    		m_background1->setPositionY(0);
    
    	m_background3->setPositionY(m_background3->getPositionY() + OFFSET);
    	m_background4->setPositionY(m_background3->getPositionY() - m_background3->getContentSize().height);
    	if (m_background4->getPositionY() >= origin.y)
    		m_background3->setPositionY(0);
    
    }




  • 相关阅读:
    linux下XAMP集成开发环境搭建流程总结
    一行代码解决各种IE兼容问题,IE6,IE7,IE8,IE9,IE10 http://www.jb51.net/css/383986.html
    安装软件时出现错误提示:无效类
    新建.xlxs文件打不开,旧的可以打开的解决办法
    PostgreSQL 自定义自动类型转换(CAST) 删除用 drop function integer_to_text(integer) CASCADE;
    php扩展不能加载的原因
    remote_addr(::1)不返回IPv4地址127.0.0.1的解决办法
    php转换字符串编码 iconv与mb_convert_encoding的区别
    PHPExcel获取CSV文件数据不准确,用以下方法获取
    日文SJIS编码字符串字符数获取方法
  • 原文地址:https://www.cnblogs.com/yutingliuyl/p/7110744.html
Copyright © 2011-2022 走看看