0

In Xcode 8, sprite kit level editor, there is a component inspector at the right panel. what is for ?

Also, when you create just a new spritekit project, you got this line of code, at line 26 in GameViewController.swift :

// Copy gameplay related content over to the scene
sceneNode.entities = scene.entities

Where are these scene.entities ?? If they don't exist, why we don't get nil and an error ??

Thank you for your help

hungry
  • 174
  • 8

2 Answers2

0

The component editor in the SpriteKit Scene Editor is a way to automatically set up GameplayKit components (GKComponents) - and a corresponding GameplayKit Scene (GKScene) object to contain them - that match your SpriteKit scene.

If you load your SpriteKit scene using GKScene(fileNamed: "foo.sks"), then what you'll get back is a GKScene object that has been pre-configured to have a SpriteKit scene (SKScene) that has all the SKNodes set up in the scene PLUS a series of GKEntities that represent the nodes in the scene file. Each of these entities have any components you defined for that node in them (which are GKComponent objects), and also a GKSKNodeComponent that helps link the GameplayKit entity to the SpriteKit node.

You can get access to your SKScene using the GKScene object's "rootNode" property. You can treat that object just as you already do as if you had loaded the scene with SKScene(fileNamed:"foo.sks"), but now you also have the GKScene that contains logical, behavior-specifying objects that can help organize a more complex project.

Why not just attach components to SKNodes? Because GameplayKit scenes are abstract entities that do not necessarily have an understanding of SpriteKit. Indeed, you could use them for any other type of display (like SceneKit); they merely provide the framework for an Entity-Component system, but it is agnostic to the manner in which the game is displayed.

So in order to tie these together and make those behaviors inspectable and editable in the sprite kit editor, this structure is put in place to separate the "logical" behavior (GKEntity/GKComponent) from the "display" behavior (SKNode). The GKSKNodeComponent is the "glue" that associates the two.

Thus, the line:

sceneNode.entities = scene.entities

...is just giving the scene a "shortcut" to the entities in the GKScene, presumably so that the GKScene object itself could be discarded. It's not a necessary part of the workflow; it's more of a convenience. That's fine if you just need to set some properties of the SKNodes in your scene, but is probably defeating the purpose for more complicated projects; in that latter case, it might be better to just keep the GKScene object around and reference its rootNode whenever you need to go into the SpriteKit scene.

For more information, refer to the GameplayKit reference in the Apple Developer Documentation.

cc.
  • 3,041
  • 1
  • 21
  • 24
-1

You can create sprites in the level editor and reference them in your code. First, go to the level editor and create a sprite. Open the attributes inspector for the level editor and give your sprite a name. Like "square". Now, in GameScene.swift, add a property for your sprite:

var mySquare: SKNode!

Then, in didMove(to view:) you can connect your property to the sprite in the editor like this:

mySquare = childNode(withName: "square")

And that should get you started.

squarehippo10
  • 1,855
  • 1
  • 15
  • 45
  • well, you are right if we want to get a reference to a sprite in the scene. But this is not the purpose of my question( even if I appreciate your help ). Did you notice the component inspector when you click on a sprite ? – hungry Jun 19 '17 at 20:03