Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Build a Game with Sprite Kit Actions and Animations Shooting Projectiles using SKAction: Part 1

J R
J R
2,037 Points

SpaceCat child

Hello, can someone help me to understand the difference between these two pieces of code that are in the GamePlayScene.m

  1. SpaceCatNode *spaceCat = [SpaceCatNode spaceCatAtPosition:CGPointMake(machine.position.x , machine.position.y-2)]; [self addChild:spaceCat];

  2. SpaceCatNode spaceCat = (SpaceCatNode) [self childNodeWithName:@"spaceCat"]; [spaceCat performTap];

I understand the first one is added to create the spaceCat in the view, however, the 2nd I don't understand why do we need to create a local variable inside " shootProjectileTowardsPosition".

1 Answer

The first line instantiates a spaceCat object just like any other alloc init or convenience constructor

The second line searches the receiver (which is self) to see if it has any children with the name "spaceCat". If a child is found it will call performTap.

We do this so that both variables are pointing to the same instance of spaceCat. There is only one spaceCat on screen, therefore instantiating multiple spaceCats will cause issues.