1

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

这在 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 中没有看到允许我这样做的构造函数......

这在更新()中:

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);
    }
}

我将不胜感激,在此先感谢。

4

1 回答 1

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

}
于 2013-08-01T07:23:09.830 回答