Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I obtain the SKSpriteNode of an item created through SpriteKit Editor?

I have created a fairly simple (at this point, experimental) "game" in XCode using SpriteKit (with Objective-C).

I know how to manually create a SKSpriteNode object and add it to the SKScene - but I am kind of trying to do the opposite - where I created the sprite inside the XCode SceneKit GUI itself (and gave it a name), now I want to retreive it in my code.

In prior experience with XCode, you would do this by declaring a @property UIOutlet [etc] and then connecting it as a referencing outlet inside the IB/Storyboard UI - but I can't find any similar way to do that inside the SpriteKit UI.

I can create a property (as an SKSpriteNode), and name it the same as my SpriteKit object - and it shows up under the SKScene (in the debugger), but is "nil" - i.e. is not "associated" with the object I created in the XCode SpriteKit UI.

Am I missing something - or doing this completely wrong? :-O

like image 631
Brad Avatar asked Nov 05 '14 18:11

Brad


1 Answers

If you set the name property of a node in the Sprite Kit Editor, you can access the node, in Swift, by

if let node = self.childNodeWithName("NodeName") as? SKSpriteNode {
    // Set up your sprite here
}

or

SKSpriteNode *node = (SKSpriteNode *)[self childNodeWithName:@"NodeName"];

in Obj-C.

like image 105
0x141E Avatar answered Sep 22 '22 09:09

0x141E