Question

So I have my main view where I can do things such as this:

player = [Hero spriteNodeWithImageNamed:@"player.png"];
player.position = CGPointMake(512, 384);
[self addChild:player];

This is done in the GameScene, which is my main scene, its loaded in the view controller like so:

-(void) presentFirstScene
{   // create and present first scene
    GameScene* myScene = [GameScene sceneWithSize:self.view.bounds.size];
    [self.kkView presentScene:myScene];
}

Now If I have a different class called A (an instance of this class can be found in GameScene) how can I add SKSpriteNodes in A to the main GameScene.

What is easiest/most efficient way of doing this?

Thanks,

Lewis

Was it helpful?

Solution 2

If A is generating nodes without GameScene explicitly requesting them, then you could use the delegate pattern. Have A call a method on its delegate when it has a new node to add.

OTHER TIPS

You can create method in your class A which returns SKSpriteNodes, for example:

// in .m
-(SKSpriteNode*)getHero {
    SKSpriteNode *hero = [SKSpriteNode spriteWithImageNamed:@"player.png"];
    // Add more customisation here
    return hero;
}

And in your GameScene you can call it like that:

SKSpriteNode *hero = [a getHero];
hero.position = CGPointMake(512, 384);
[self addChild: hero];

Remember to add class declaration to .h file:

-(SKSpriteNode*)getHero;

Hope this what you are after.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top