0

So I created a class called Berries that extends CCSpriteBatchNode. The Berries are basically like coins in my game, adding to the score value if 'collided' with. This is the code I have in my Berries class for CCSpriteBatchNode:

- (id) init
{
    if((self = [super init]))
    {
        CCSpriteBatchNode* berryBatch1 = [CCSpriteBatchNode batchNodeWithFile:@"One.png"];
        [self addChild:berryBatch1];

        CCSpriteBatchNode* berryBatch2 = [CCSpriteBatchNode batchNodeWithFile:@"Two.png"];
        [self addChild:berryBatch2];

        CCSpriteBatchNode* berryBatch3 = [CCSpriteBatchNode batchNodeWithFile:@"Three.png"];
        [self addChild:berryBatch3];

        for (int i = 0; i < 100; i++)
        {
            CCSprite* berry1 = [CCSprite spriteWithFile: @"One.png"];
            [berryBatch1 addChild:berry1];

            CCSprite* berry2 = [CCSprite spriteWithFile: @"Two.png"];
            [berryBatch2 addChild:berry2];

            CCSprite* berry3 = [CCSprite spriteWithFile: @"Three.png"];
            [berryBatch3 addChild:berry3];
        }
        numBerries = 
    }
    return self;
}

numBerries is an int that I made. I am trying to figure out the number of berries in all three CCSpriteBatchNodes so that when I make a detectCollision method I can use it in the for loop.

Any ideas?

Surz
  • 984
  • 3
  • 11
  • 36
  • Why is Berries a subclass of CCSpriteBatchNode? Looks to me as if subclassing CCNode is absolutely sufficient here. – CodeSmile Mar 16 '13 at 01:35
  • Because the class is all about creating a CCSpriteBatchNode? – Surz Mar 16 '13 at 02:04
  • But you're not using it as a sprite batch node. This is not what inheritance is used/useful for! You want to inherit if your subclass "is like the super class" but not when it's "using the super class". You have created an aggregate class, which is the right way to do what you want to do, but you should not subclass the class you're merely using inside your class. Change it to subclass from CCNode. Does the Berries class still work? Yes. Therefore subclassing CCSpriteBatchNode is not needed. Plus you can now add other nodes to Berries (CCSpriteBatchNode only allows CCSprite children). – CodeSmile Mar 16 '13 at 09:58
  • Oh okay wow just like Java! Thank you so much for all the help! – Surz Mar 16 '13 at 20:24

1 Answers1

0

It's simple:

numBerries = berryBatch1.children.count + 
             berryBatch2.children.count + 
             berryBatch3.children.count;
CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • Thanks! Unfortunately I'm getting the error, 'CCSpriteBatchNode only supports CCSprites as children', with my CCSpriteBatchNode :/ – Surz Mar 16 '13 at 01:39
  • See my comment on the question. This is because you subclassed CCSpriteBatchNode when you shouldn't have. Subclass CCNode instead. Your Berries class is not a sprite batch node because your intention isn't to change/improve how sprite batching works, ie how it renders sprites to the screen and other sprite-batching logic. – CodeSmile Mar 16 '13 at 10:01