背景图至上往下无限滚动
// 背景图中障碍物, 由tiled编辑的 m_backTileMap = CCTMXTiledMap::create("level01.tmx"); addChild(m_backTileMap, -9); m_backTileMapHeight = m_backTileMap->getMapSize().height * m_backTileMap->getTileSize().height; //获取tile map的高度 //刚开始就称动 m_backSkyHeight -= 48; m_backTileMapHeight -= 200; m_backSky->runAction(CCMoveBy::create(3, ccp(0, -48))); //为背景图移动 m_backTileMap->runAction(CCMoveBy::create(3, ccp(0, -200)));//障碍物移动 m_isBackSkyReload=false; m_isBackTileReload=false; schedule(schedule_selector(HelloWorld:: movingBackground),3);
3秒执行调用一次,背景图3秒持续称动一个位置,当第一张背景图和障碍物图项部超过屏幕顶时,第二张图接上3秒执行调用一次,背景图3秒持续称动一个位置,当第一张背景图和障碍物图项部超过屏幕顶时,第二张图接上
void HelloWorld::movingBackground(float dt) { m_backSky->runAction(CCMoveBy::create(3, ccp(0, -48))); m_backTileMap->runAction(CCMoveBy::create(3, ccp(0, -200))); // 每次移动48 m_backSkyHeight -= 48; // 每次移动200 m_backTileMapHeight -= 200; // 若背景图的顶部到达屏幕顶部或在顶部以下 if (m_backSkyHeight <= winSize.height) { if (!m_isBackSkyReload) { // 如果另一张图还没加载则create一个 m_backSkyRe = CCSprite::create("bg01.jpg"); m_backSkyRe->setAnchorPoint(ccp(0, 0)); addChild(m_backSkyRe, -10); m_backSkyRe->setPosition(ccp(0, winSize.height));//第二图与第一图连接上 // 反转标志位 m_isBackSkyReload = true; } // 第二张图紧接着第一张图滚动 m_backSkyRe->runAction(CCMoveBy::create(3, ccp(0, -48))); } // 若第一张背景图完全经过屏幕 if (m_backSkyHeight <= 0) { m_backSkyHeight = m_backSky->getContentSize().height; // 移除第一张的精灵 this->removeChild(m_backSky, true); // 指向第二张图的精灵 m_backSky = m_backSkyRe; // 第二张的精灵指针置空 m_backSkyRe = NULL; // 反转标志位 m_isBackSkyReload = false; } //若障碍物图到达屏幕顶部 if (m_backTileMapHeight <= winSize.height) { //若第二障碍物图没有加载则创建 if (!m_isBackTileReload) { m_backTileMapRe = CCTMXTiledMap::create("level01.tmx"); this->addChild(m_backTileMapRe, -9); m_backTileMapRe->setPosition(0, winSize.height); m_isBackTileReload = true; } m_backTileMapRe->runAction(CCMoveBy::create(3, ccp(0, -200))); } //若障碍物图完全经过屏幕 , m_backTileMap指向第二张 if (m_backTileMapHeight <= 0) { m_backTileMapHeight = m_backTileMap->getMapSize().height * m_backTileMap->getTileSize().height; this->removeChild(m_backTileMap, true); m_backTileMap = m_backTileMapRe; m_backTileMapRe = NULL; m_isBackTileReload = false; } }