I’m beginning to learn all about game development,
i would like to know when i should declare an object in the scene as a SKSpriteNode( ) and when to declare it as an SKNode( ).
I know that there is a difference between them,
and i know that i should declare an SKNode( ) when i have multiple objects that i should add as Children to this node in order to control their physics by controlling the physics of the Node.
But i can’t seem to find the purpose of using an SKNode( ) when only one object is added as a child (SKSpriteNode( ) ) of this Node.
A SKNode is an empty node with no visual representation on the screen. As you mentioned, the typical use of SKNode is when you want to have a bunch of nodes with a shared parent, so you can move them all around at once. For example, you might have a SKNode for the background layer and another for the foreground layer.
A SKSpriteNode is when you have a visual sprite that you want to show up on the screen (like a main character or an enemy). SKSpriteNodes can also have children - for example a SKSpriteNode for a car might have some other SKSpriteNode children for wheels.
Let me know if this helps, or if you have any other questions!
Thank you for your quick reply,
what you said really helped but i originally asked my question because in the games tutorial, when we are making the game zombies, we declared the player as an SKNode( ), i didnt see the purpose of that since the player is only one object.
In the tutorial found in the wrotten tutorial on your website, how to make a game like mega jump, the player node was declared as an SKNode( ) with only one child, the player. What is the purpose of doing this.
@dfeg I checked 2D iOS Games by Tutorials and we use a SKSpriteNode for the zombie - I think you might be talking about the Mega Jump tutorial.
Looking at the Mega Jump tutorial, I’m not sure why the author decided to use a SKNode with a SKSpriteNode child rather than just using a SKSpriteNode directly. As far as I can tell, it would work fine either way in this case.
Generally I prefer to use the simplest possible way that works so probably would have used the SKSpriteNode directly rather than adding the intermediate layer.
By ‘interact with’, I’m assuming you mean collision detection and such?
Often in Sprite Kit games you use the physics engine to handle collision detection. In that case it depends where you attach the physicsBody to. Sprite Kit notifies you when two physicsBody’s collide, not when two nodes collide.
Indeed… Thanks… if the SKSpriteNode is a child of a SKNode, how do you act upon the SKSpriteNodes colliding? OR, when the collision is detected, the Physics Engine is actually reporting the collision between the SKSpriteNodes within the SKNode?
Sprite Kit Layer - What you see on screen - SKSpriteNodes, etc.
Physics Layer - Virtual shapes the physics engine uses for collision detection.
You connect the two by setting a physics body on a Sprite Kit node. Sprite Kit then makes sure the position of the physics body matches the position of the node.
So - if you want to tell when two SKSpriteNodes collide, you would create a physics body for each SKSpriteNode, and set their contactTestBitMask appropriately. Sprite Kit will then call the didBeginContact when the two bodies corresponding to the sprite nodes collide.
IT does… after doing a bit more reading, I think my issue was with the 3 components that are used during collision detection. - Which each is used where how they are bound etc… Which ones are actually used …
anyway, I would recommend a brief tutorial about how that all works, especially if both ‘colliders’ need to react as opposed to just disappearing.
Thanks for you and your partners work - you guys have helped me a great deal.