2

I don't understand why the following is happening and I hope someones here can explain it.

I have a GameLayer (CCLayer) Class and a Food (CCNode) Class.

In the Gamelayer Class I create a bunch of food Objects that have a sprite as a property. And I want to add these sprites to a CCSpriteBatchNode.

spriteBatch = [CCSpriteBatchNode batchNodeWithFile:@"bol.png"];
[self addChild:spriteBatch]; 
for (int i = 0; i < 1000; i++) {

   Food * tempfood = [Food foodWithParentNode:self];

   [spriteBatch addChild:[tempfood mySprite]];

   [self addChild:tempfood];

 }

When I use the code above, the sprites all show up on screen but don't move. (They should because I scheduled an Update in the Food Class (see below) and in this update the position of the food objects is changed)

-(id)initWithParentNode:(CCNode *)parentNode{

if((self = [super init]))
{
    mySprite = [CCSprite spriteWithFile:@"bol.png"];

    CGSize screenSize = [[CCDirector sharedDirector] winSize];

    [[self mySprite] setPosition:CGPointMake(screenSize.width/2, screenSize.height/2)];

    [self scheduleUpdate];

}

return self;
}
-(void) update:(ccTime)delta
{
  ... DO SOME position calculations ...

 [[self mySprite] setPosition:CGPointMake(newX, newY)];
}

But if I move the code that adds the sprite to the batch from the Game class to the food class the update that changes the position does work and the foods are moving on screen. BUT WHY?

So this gives:

-(id)initWithParentNode:(CCNode *)parentNode{

if((self = [super init]))
{
    mySprite = [CCSprite spriteWithFile:@"bol.png"];

    CGSize screenSize = [[CCDirector sharedDirector] winSize];

    [[self mySprite] setPosition:CGPointMake(screenSize.width/2, screenSize.height/2)];

    [[ (GameLayer*) parentNode spriteBatch] addChild:mySprite];

    [self scheduleUpdate];

}

return self;
}

I really can't see the difference between calling

[[ (GameLayer*) parentNode spriteBatch] addChild:mySprite];

from the food class or :

[spriteBatch addChild:[tempfood mySprite]];

from the 'parent' GameLayer

Ruben O
  • 85
  • 6
  • make you sure that mySprite property points to the mySprite instance? maybe for some reason it is nil at the moment you want to add it from your game layer – Morion Apr 01 '13 at 08:43
  • It's definitely not nil because it shows up on screen but the scheduled update is not firing so it's not moving... – Ruben O Apr 01 '13 at 14:59
  • You cannot say that it is "definitely not nil", because this is just a property. You could create sprite(mySprite variable in your Food class), but declare property a bit wrong, so foodInstance.mySprite could be nil. – Morion Apr 01 '13 at 18:35

1 Answers1

1

Ruben, the mySprite is a property with retain attribute? The Food class can be loosing the memory reference of this property...

on init, try to set mySprite using self.mySprite, to retain this.

on .m or .h, put:

@property (nonatomic, retain) CCSprite *mySprite

and on init, use:

self.mySprite = [CCSprite spriteWithFile:@"bol.png"];
Bivis
  • 1,320
  • 1
  • 10
  • 24