前端之家收集整理的这篇文章主要介绍了
cocos2dX-地图滚动,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
bool ScrollMap::init()
{
if (!CCLayer::init())
{
return false;
}
do
{
CCSprite * map1 = CCSprite::create("Images/gir/BG1.png");
CCSprite * map2 = CCSprite::create("Images/gir/BG1.png");
map1->setTextureRect(CCRectMake(0,winSize.width,winSize.height));
map2->setTextureRect(CCRectMake(0,winSize.height));
addChild(map1,MAP_1_Tag);
addChild(map2,MAP_2_Tag);
CCSize mapSize = map1->getContentSize();
map1->setPosition(ccp(0,0));
map2->setPosition(ccp(map1->getPositionX()+mapSize.width,0));//map1的宽加map2的宽;
map1->setAnchorPoint(ccp(0,0));
map2->setAnchorPoint(ccp(0,0));
//CCLog("%f",map1->getContentSize().width+origin.x);
scheduleUpdate();
} while (0);
return true;
}
void ScrollMap::update(float dt)
{
CCSprite * temp1 = (CCSprite *)getChildByTag(MAP_1_Tag);
CCSprite * temp2 = (CCSprite *)getChildByTag(MAP_2_Tag);
temp1->setPositionX(temp1->getPositionX() - 5);
temp2->setPositionX(temp2->getPositionX() - 5);
if (temp2->getPositionX()<=0)
{
temp1->setPositionX(temp2->getPositionX() + temp1->getContentSize().width);
}
if (temp1->getPositionX()<= 0)
{
temp2->setPositionX(temp1->getPositionX() + temp2->getContentSize().width);
}
}
void ScrollMap::onExit()
{
unscheduleUpdate();
CCLayer::onExit();
}
原文链接:https://www.f2er.com/cocos2dx/345671.html