3
votes

I've ready many places that tell me I shouldn't inherit the CCSprite class and rather just add a sprite to my class as a member variable.

Here is what I've done: Created a CCNode class, Added a CCSprite variable to my node class, Initialized my sprite and added it as a child to my node.

I now have a CCLayer where I want to draw these sprites but when I add my node as a child nothing is drawn. If I add the nodes sprite as the child rather than the node it will draw.

My question is, is there anyway to get my sprites to draw from adding the node as a child to the layer or is adding the sprites the only way?

I'm new to Objective-c and iPhone dev so any help will be greatly appreciated.

Edit Here is my CCNode Class - animalSprite is my CCSprite.

    @implementation Animal

    @synthesize animalSprite = _animalSprite;

    -(Animal *) initWithFile:(NSString *)texture position:(CGPoint) position
    {
        self.animalSprite = [CCSprite spriteWithFile:texture];
        self.animalSprite.position = position;
        self.animalSprite.scale = 0.25f;
        [self addChild:self.animalSprite];

        return self;
    }

    -(void) dealloc{

        [super dealloc];
    }

@end

Now in my CCLayer class I have a level variable that contains a list of my nodes.

//Load Level
Level *level = [LoadLevel loadLevel];

for (Animal *animal in level.animals)
{
    [self addChild:animal];
}

Edit I create my node objects and then add them to the array in the levels class the following way.

Animal *animal = [[[Animal alloc] initWithFile:animalFileName position:position] autorelease];
[level.animals addObject:animal];
1

1 Answers

0
votes

Try reading this thread http://www.cocos2d-iphone.org/forum/topic/11566

Hopefully you have the same issue where the sprite is being released before you use it.

Cheers,

Taz

Latest game Chained: http://www.silverbacktechsol.co.uk/stschained.html