2

I have a background that extends CCSprite from the cocos2d framework. And I have added this sprite to the gamelayer. Now in this background class I try to add other CCSprites named Star like so:

//create the stars
stars = [[CCArray alloc] init];
for (int i = 0; i < 10; i++) 
{
    Star* star = [[Star alloc ] initWithFile:@"star-hd.png"];
    CGSize screensize = [[CCDirector sharedDirector] winSize];
    //CCLOG(@"screensize: %f, %f", screensize.width, screensize.height);
    CGPoint newstarlocation;
    newstarlocation.x = CCRANDOM_0_1() * screensize.width;
    newstarlocation.y = CCRANDOM_0_1() * screensize.height;
    star.position = newstarlocation;
    [self addChild:star z:i];
    [stars addObject:star];
}

but the stars won't show. I tried several things and the only thing that seems to work is when I add the stars on the gamelayer instead of the background. but that is not what I want.

is it not allowed in cocos2d to nest sprites? and if it is allowed, how do I nest sprites?

P.J.Radadiya
  • 1,493
  • 1
  • 12
  • 21
Marnix v. R.
  • 1,638
  • 4
  • 22
  • 33
  • Code seems correct, try using a different image (star.png) and fixed positions (position of stars are *relative* (ie offsets) to the position of the parent (the background sprite). Finally, what's the point in adding the stars to their own array? After all, you can just use the children array where you add the stars to. If necessary give them all the same tag to differentiate between other nodes. – CodeSmile Feb 13 '12 at 20:14

3 Answers3

1

You can't nest sprites, but you can (should) nest layers.

Create a background layer instead, add the background sprite and star sprites to it, and add the background layer to the scene before the gamelayer. That way you keep the background apart from the gamelayer and behind everything else, with as many sprites as you want.

Lazy example (in C++):

CCLayer *backgroundLayer = CCLayer::create();
CCSprite *skySprite = CCSprite::createWithSpriteFrameName("sky_sprite.png"); 
CCSprite *starsSprite = CCSprite::createWithSpriteFrameName("stars_sprite.png");

backgroundLayer->addChild(skySprite);
backgroundLayer->addChild(starsSprite);

CCLayer *gameLayer = CCLayer::create();

scene->addChild(backgroundLayer);
scene->addChild(gameLayer);    
bakkay
  • 748
  • 8
  • 16
1

Just to further clarify Steffen's point (who by the way has one of the best tut books out there). The stars probably will not show up calling them "star-hd.png". Rather you should reference "star.png". Cocos2d looks for the suffixes '-hd' and '-ipad' automatically on your images to see which image should be associated with which device. You can change what suffix the program looks for by looking at the AppDelegate.m file under, which you'll find this code.

suffixes are going to be used
[sharedFileUtils setiPhoneRetinaDisplaySuffix:@"-hd"];      // Default on iPhone RetinaDisplay is "-hd"
[sharedFileUtils setiPadSuffix:@"-hd"];                 // Default on iPad is "ipad"
[sharedFileUtils setiPadRetinaDisplaySuffix:@"-ipadhd"];    // Default on iPad RetinaDisplay is "-ipadhd"
Real Talk
  • 60
  • 8
-1

I don't see why you would want to nest sprites, and not only that, would it be very efficient. Write a Star class that contains the sprite, and the child sprites.

It is allowing you to do it because cocos2d loves CCNode, almost everything derives from it. That doesn't mean that CCSprite handles drawing their children. Both the CCLayer and CCSprite can have CCNodes added. It's just their handlers are different.

You would also be a bit more efficient in that, because then you could sprite batch, which is a lot more efficient than drawing sprites directly to the game layer.

iHunter
  • 6,205
  • 3
  • 38
  • 56
Ryan Copley
  • 873
  • 1
  • 10
  • 26