cocos2d-iphoneccspritebatchnode

Adding sprites to a Cocos2d CCSpriteBatchNode seems to ignore the position


As a test I've added some sprites to a batch node. They all draw at 0,0, it seems to ignore the sprite's position, which I thought would now be relative to the batch node. What am I missing?

CCLayer* splash = [[CCLayerColor alloc]initWithColor:ccc4(255,255,255,255)];
[self addChild:splash];

CCSpriteFrame* cf = [[CCSpriteFrameCache sharedSpriteFrameCache]spriteFrameByName:@"circle.png"];

CCSpriteBatchNode* batch = [CCSpriteBatchNode batchNodeWithTexture:cf.texture];

CCSprite* test = [CCSprite  spriteWithSpriteFrameName:@"circle.png"];
test.position = CGPointMake(20,20);
test.color = ccc3(255,255,0);
[batch appendChild:test];
test = [CCSprite  spriteWithSpriteFrameName:@"square.png"];
test.position = CGPointMake(60,60);
test.color = ccc3(255,0,0);
[batch appendChild:test];
test = [CCSprite  spriteWithSpriteFrameName:@"square.png"];
test.position = CGPointMake(100,60);
test.color = ccc3(255,125,125);
[batch appendChild:test];
test = [CCSprite  spriteWithSpriteFrameName:@"bomb.png"];
test.position = CGPointMake(100,100);
test.color = ccc3(255,0,255);
[batch appendChild:test];
[splash addChild:batch];

Solution

  • I've never seen [batch appendChild: ] being used before. on the cocos2d class reference I couldn't find it. Have you tried [batch addChild: child]; ? There might also be an issue with adding the same CCSprite several times, try making a new sprite for each image and adding it.

    For example:

        CCSprite* test = [CCSprite  spriteWithSpriteFrameName:@"circle.png"];
        test.position = CGPointMake(20,20);
        test.color = ccc3(255,255,0);
        [batch addChild:test];
        CCSprite* second = [CCSprite  spriteWithSpriteFrameName:@"square.png"];
        second.position = CGPointMake(60,60);
        second.color = ccc3(255,0,0);
        [batch addChild:second];