码迷,mamicode.com
首页 > 其他好文 > 详细

Cocos2d-X循环背景效果

时间:2014-11-11 07:03:19      阅读:368      评论:0      收藏:0      [点我收藏+]

标签:style   blog   io   color   ar   os   使用   sp   div   

准备一张两边可循环连接的图,使用下面代码即可实现~

HelloWorld::bd(0, 0, 98);
HelloWorld::bd(0, 2130, 99);
this->schedule(schedule_selector(HelloWorld::move));
void HelloWorld::bd(float num, int w, int tag)
{
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    float x = size.width, y = size.height + origin.y;

    CCSprite *background = CCSprite::create("bg.png");
    background->setAnchorPoint(CCPoint(0, 0.5));
    background->setPosition(CCPoint(background->getContentSize().width*num + w, y / 2));
    this->addChild(background, tag, tag);
}

void HelloWorld::move(float time)
{
    CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
    CCSize size = CCDirector::sharedDirector()->getWinSize();
    float x = size.width, y = size.height + origin.y;

    CCNode *bg1 = CCNode::getChildByTag(99);
    CCSprite *sp1 = (CCSprite*)bg1;

    CCNode *bg2 = CCNode::getChildByTag(98);
    CCSprite *sp2 = (CCSprite*)bg2;

    sp1->setPosition(CCPoint(sp1->getPositionX() - 0.5, sp1->getPositionY()));
    sp2->setPosition(CCPoint(sp2->getPositionX() - 0.5, sp2->getPositionY()));
    if (sp1->getPositionX() <= -sp1->getContentSize().width) sp1->setPosition(CCPoint(sp1->getContentSize().width, y / 2));
    if (sp2->getPositionX() <= -sp2->getContentSize().width) sp2->setPosition(CCPoint(sp2->getContentSize().width, y / 2));
}

 

Cocos2d-X循环背景效果

标签:style   blog   io   color   ar   os   使用   sp   div   

原文地址:http://www.cnblogs.com/Susake/p/4088610.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!