xcodeswiftcocos2d-iphonespritebuilder

Swift - Get CCNode from scene


I've tried to search on the internet for a method that can get a specific node from the scene. I've seen a lot of methods, but it doesn't give me a clear answer.

I've have build a scene in SpriteBuilder from Cocos2D. This is how the MainScene.ccb looks like:

MainScene

If needed: This is how Player.ccb looks like:

Player

I'm trying to get the player 'object' in Xcode after I published the project. I've tried to use CCBReader, but I can't find any useful method (unless I missed it). Also I've tried so use self.children, but I don't know how to continue any further with that.

Can you help me out? At the end, I want to get the position of the player.

Thanks!

By the way, I'm a beginner in Swift, so don't expect that I know all of the terms.


Solution

  • When you add nodes (sprite) to your sprite builder project, make sure it is selected, then on the right, click theorem code connections tab. In the 'Doc root var' type your variable name in the box.

    When you load Xcode select the file that loads the scene from sprite builder and you can then declare a variable in this file with the same name you gave it in sprite builder. You will then be able to use that node (sprite) whenever you want and access it's properties.