1

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 PDEnemys. 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.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Charlie Schliesser
  • 7,851
  • 4
  • 46
  • 76
  • Odds are you've got a different instance of the object behind _world each time. – Hot Licks May 08 '14 at 16:40
  • 1
    What about the debugger? Forget `NSLog`s, put a breakpoint there, use `po _world` and `po enemy` to see what's going on there. – Sulthan May 08 '14 at 16:44
  • @HotLicks by "behind _world" do you mean the zPosition? I've verified that's not the issue. `_world` is at 0 and all enemy sprites are 50. – Charlie Schliesser May 08 '14 at 17:02
  • @Sulthan that's very cool. `po` pointed me towards http://stackoverflow.com/questions/11513283/how-to-print-out-a-propertys-contents-using-xcode-debugger and I've discovered how easy it is to inspect elements during runtime. The memory address is different for each enemy that's returned, so I know that's functioning correctly. Perhaps something else is being clobbered... I'll keep investigating. – Charlie Schliesser May 08 '14 at 17:06
  • Can you post the code that shows what you need the "intoScene" for? Are you sure that the enemy nodes don't simply overlap each other and thus look like a single instance? – CodeSmile May 08 '14 at 17:06
  • Then perhaps `addChild` clobbers whatever child was there before? – Hot Licks May 08 '14 at 17:17
  • 1
    You can simply check the nodes on the scene by logging self.children from the scene. – ZeMoon May 09 '14 at 06:10
  • That all helped. Ultimately the problem was with some placement of the new nodes and some random generators that weren't really being random... kind of silly, but all of the debugging comments really helped and are good to know going forward. Thank you all very much. – Charlie Schliesser May 09 '14 at 12:57

0 Answers0