cocos2d-iphoneiteratorkobold2d

CCSprite position erratic when retrieving from CCSprite subclass via iterator in Cocos2d


I'm subclassing the CCSprite. (Originally was subclassing CCNode but had same issue)

from EnemyShip.h:

@interface EnemyShip : CCSprite
- (id)initWithFile:(NSString *)file health:(int)health;

@property (nonatomic, assign) NSInteger health; 

@end

@interface easyShip : EnemyShip
- (id)init;
@end

from EnemyShip.mm:

@implementation EnemyShip

@synthesize health;

- (id)initWithFile:(NSString *)file health:(int)h
{
    if ((self = [super initWithFile:file])) {
        self.health= h;
        self.position=[CCDirector sharedDirector].screenCenter;
}
return self;
}

@end

@implementation easyShip

- (id)init{
    if ((self = [super initWithFile:@"enemy_side_hole.png" health:100])){}
    return self;
}

@end

All pretty basic. I'm adding this enemy via:

-(void) addEnemy:(CGPoint)p
{
    EnemyShip *enemyShip = [[easyShip alloc]init];
    [self addChild:enemyShip z:0]; 
}

So I'm doing collision detection in my update method. (Again - once this works I'll subclass CCNode with a sprite property etc etc)

for(EnemyShip* theEnemies in [self children])
{
    NSLog(@"x,y: (%f,%f)",theEnemies.position.x,theEnemies.position.y);
}

What I understand the above to do it access all 'EnemyShip' I've added as children and Log it's position. It does. Except the positions are all over the place - even though I can see the sprite not moving anywhere and an NSLog from the class on the same .position.x/y outputs the correct coordinants.

My output is:

2013-04-23 23:24:48.122 Radon[9209:907] x,y: (0.000000,0.000000)
2013-04-23 23:24:48.130 Radon[9209:907] x,y: (768.000000,512.000000)
2013-04-23 23:24:48.131 Radon[9209:907] x,y: (384.000000,512.000000)
2013-04-23 23:24:48.134 Radon[9209:907] x,y: (192.000000,512.000000)
2013-04-23 23:24:48.135 Radon[9209:907] x,y: (600.000000,30.000000)
2013-04-23 23:24:48.135 Radon[9209:907] x,y: (525.000000,30.000000)
2013-04-23 23:24:48.136 Radon[9209:907] x,y: (450.000000,30.000000)
2013-04-23 23:24:48.138 Radon[9209:907] x,y: (375.000000,30.000000)
2013-04-23 23:24:48.139 Radon[9209:907] x,y: (300.000000,30.000000)
2013-04-23 23:24:48.140 Radon[9209:907] x,y: (225.000000,30.000000)
2013-04-23 23:24:48.142 Radon[9209:907] x,y: (162.000000,512.000000)

// - Edited to reflect a typo and incorrect output as a result


Solution

  • I've used an alternate approach, as I need this done quickly.

    added

    NSMutableArray enemies;
    

    create 'enemies':

    EnemyShip *enemyShip = [[EnemyShip alloc]init];
    [enemies addObject:enemyShip];
    [self addChild:enemyShip z:0];
    

    iterate through this specifically instead of grabbing the other way:

    for(NSUInteger i=0;i<[enemies count];i++){
        EnemyShip* enemy = [enemies objectAtIndex:i];
        NSLog(@"x,y: (%f,%f)",enemy.sprite.position.x,enemy.sprite.position.y);
    }
    

    Works fine.

    Note: ^ using a CCNode subclass which contains a CCSprite instead of a CCSprite subclass, hence some names are different.