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

流过昼夜 提交于 2019-12-21 11:01:14

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/26764267/how-do-i-obtain-the-skspritenode-of-an-item-created-through-spritekit-editor

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!