I'm fairly new to Objective C. I have a class PDScene : SKScene
which inits a new instance (or so I thought) of PDEnemy : SKNode
. Here's the code with irrelevant lines removed:
PDScene.m
-(void)enemySpawn:(NSString*)name
{
PDEnemy *enemy = [[PDEnemy alloc] initEnemyNamed:name intoScene:self];
NSLog(@"%@", enemy);
[_world addChild:enemy]; // _world is simply an SKNode
}
PDEnemy.m
-(id)initEnemyNamed:(NSString*)name intoScene:(PDScene*)scene
{
if (self = [super init]) {
NSLog(@"Init enemy '%@'", name);
... // build out SKSpriteNode, init actions, etc.
}
return self;
}
The issue I'm running into is that additional instances of PDEnemy
overwrite the previous instance. For example, if I call [self enemySpawn:@"somEnemyName"]
5 times, I can see 5 sets of NSLog
in my console, but I only have 1 PDEnemy
on screen. If I change enemySpawn:name
to:
[_world addChild:[[PDEnemy alloc] initEnemyNamed:name intoScene:self]];
then it works as expected and I get 5 different PDEnemy
s. There are lots of places in my code where I have methods that use the same object instance identifier (e.g. laser
, bullet
) and they don't overwrite each other on multiple method calls, so why is it happening on my custom class? If more code would be helpful please let me know. This could ultimately just be a tiny bug that I'm overlooking, or a more fundamental misunderstanding of obj-c.