Group Group Group Group Group Group Group Group Group

Use SKSpriteNode( ) or SKNode()

Hello,

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.

2 Likes

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!

4 Likes

Hello Ray,

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.

Which tutorial specifically are you talking about?

In the 2D iOS and tvOS games by tutorial, the first game of zombies, the playerNode was declared as an SKNode( ), is there a specific purpose to this ?

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.

1 Like

as a follow up, do the SKSpriteNodes within the SKNodes interact with other SKSpriteNode at the SKSpriteNode level or do the SKNodes do the interaction?

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.

Does that make sense?

Indeed… :slight_smile: 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?

Think of two different layers:

  1. Sprite Kit Layer - What you see on screen - SKSpriteNodes, etc.
  2. 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.

Hope that helps!

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.

Neil