如何在cocos2d-x 2.1.4中复制精灵的精灵

时间:2013-07-31 16:17:46

标签: c++ cocos2d-iphone sprite cocos2d-x

嗯,我的问题很简单,我开始使用cocos2d-x编程,我正在尝试制作平铺的无限背景,假设我有一个png的图像我想无限重复作为背景,什么我做的是我创建了一个Sprite作为容器,然后我添加了具有相同图像的精灵,一个旁边一个,覆盖了大约110%的屏幕,然后,按照太空游戏教程我再次创建了相同的sprite精灵。第二个背景,我用它作为两个大的相同图像滚动无限...它的工作完美...但我想知道是否有可能通过复制frist创建第二个精灵。有些代码可能会清除我的情况:

这是在init():

acum = 0.0;
city1 = CCSprite::create();
for(int i = 0; i < 12; ++i) {
    CCSprite *city = CCSprite::createWithSpriteFrameName("env_buildings_background2.png");
    city->setPosition(ccp(acum, 0));
    city1->addChild(city);
    acum+= city->getContentSize().width*0.99;
}
city1->setContentSize(ccp(acum, 0));
city2 = CCSprite::create();
acum = 0.0;
for(int i = 0; i < 12; ++i) {
    CCSprite *city = CCSprite::createWithSpriteFrameName("env_buildings_background2.png");
    city->setPosition(ccp(acum, 0));
    city2->addChild(city);
    acum+= city->getContentSize().width*0.99;
}
city2->setContentSize(ccp(acum, 0));
_backgroundNode->addChild(city1, -1 , buildingspeed, ccp(0, winSize.height * 0.6));

_backgroundNode->addChild(city2, -1 , buildingspeed, ccp(city1->getContentSize().width, winSize.height * 0.6));
printf("%f - %f\n", city1->getContentSize().width, city2->getContentSize().width);

主要问题在于,我需要从city1创建city2而不仅仅是重复代码......有没有办法做到这一点?我没有看到CCSprite中的构造函数允许我这样做......

这是在update():

CCArray *cities = CCArray::createWithCapacity(2);
cities->addObject(city1);
cities->addObject(city2);
for ( int ii = 0; ii <cities->count(); ii++ ) {
    CCSprite * city = (CCSprite *)(cities->objectAtIndex(ii));
    float xPosition = _backgroundNode->convertToWorldSpace(city->getPosition()).x;
    float size = city->getContentSize().width;
    if ( xPosition < -size ) {
        _backgroundNode->incrementOffset(ccp(city->getContentSize().width*2,0),city);
    }
}

我会感激任何帮助,谢谢你。

1 个答案:

答案 0 :(得分:1)

此代码将创建citySprite的确切快照。由于这是一个快照,你将无法访问它包含的精灵。因为它们现在嵌入图像或精灵本身。

float citySpriteWidth = 480;
float citySpriteHeight = 320;

//Set position in order to make it fit inside CCRenderTexture (You can change this later)
citySprite->setPosition(ccp(citySpriteWidth/2, citySpriteHeight/2));

CCRenderTexture *render = CCRenderTexture::renderTextureWithWidthAndHeight(citySpriteWidth, citySpriteWidth);
render->beginWithClear(0, 0, 0, 0);
citySprite->visit();
render->end();

CCTexture2D *tex = render->getSprite()->getTexture();
CCSprite *newCitySprite = CCSprite::spriteWithTexture(tex);
newCitySprite->setFlipY(true);  //Texture might be upside down

希望这有助于您的要求。

否则,如果您的问题只是代码重复。然后你可以写一个函数,它会给你你的城市精灵包含像这样的精灵。并称之为你想要它的次数。

CCSprite* creteCitySprite()
{
     float acum = 0.0;
     CCSprite *city1 = CCSprite::create();

     for(int i = 0; i < 12; ++i)
     {
          CCSprite *city = CCSprite::createWithSpriteFrameName("env_buildings_background2.png");
          city->setPosition(ccp(acum, 0));
          city1->addChild(city);
          acum+= city->getContentSize().width*0.99;
     }

     city1->setContentSize(CCSizeMake(acum, 0));

     return city1;
}

<强>编辑: 这给你你想要的。但我想说的是,你添加到city1的子精灵将保持静止。

 CCSptire *getSpriteFromSprite(CCSprite *citySprite, float citySpriteWidth, float citySpriteHeight)
{
    CCPoint prevPosition = citySprite->getPosition();

    //Set position in order to make it fit inside CCRenderTexture (You can change this later)
    citySprite->setPosition(ccp(citySpriteWidth/2, citySpriteHeight/2));

    CCRenderTexture *render = CCRenderTexture::renderTextureWithWidthAndHeight(citySpriteWidth, citySpriteWidth);
    render->beginWithClear(0, 0, 0, 0);
    citySprite->visit();
    render->end();

    citySprite->setPosition(prevPosition);

    CCTexture2D *tex = render->getSprite()->getTexture();
    CCSprite *newCitySprite = CCSprite::spriteWithTexture(tex);
    newCitySprite->setFlipY(true);  //Texture might be upside down

}