Matej P
Matej P

Reputation: 125

SpriteKit sprite position inside node tree

Currently working on a simple 2D game, where I have player character that is split into multiple sprites (head, torso, legs, arms,...). I have absolute coordinates right in Aseprite (if I take individual sprite and position them I get correct coordinates).

When I put everything into swift and use negative y instead of positive everything gets totally weird. For example in Aseprite I have coordinates as follow: head (30, 17), torso (30, 24) and legs (28, 35). Everything aligns perfectly.

In SpriteKit I extend SKNode class and put every subsprites inside with just negative number for y. So instead of going up, I draw down. It looks like that coordinates give in pixels are not correct - sprites are off by few pixels. Mostly is off y coordinate but in some cases (character rotation) also x.

How to get from those absolute upper-left coordinates to correct SpriteKit coordinates then?

Upvotes: 0

Views: 188

Answers (1)

Matej P
Matej P

Reputation: 125

Ok I figured what was wrong (several things):

  • your parent class can be SKNode, but children has to have set anchor point at (0,1)
  • when changing texture of child sprite, always make sure that sprite size is set to new size of texture. If not it will use previous texture size and resize new texture to old size. This introduces additional problems with positioning. So you have to call: child.size = child.texture!.size() (I used force unwrap because I set (new) texture one step before so I'm 100% sure it exists.
  • when setting new texture set anchor point again (it seems it gets reset when changing texture of child sprite).

Upvotes: 0

Related Questions