Reputation: 10021
I'm building a game where the characters have health bars. I did some quick research and found that subclassing an SKNode and adding SKSpriteNode members for the character and the health bar respectively is a good approach. So currently I have this:
class GameCharacter: SKNode {
let characterBody = SKSpriteNode()
let characterHealthBar = SKSpriteNode()
}
however I'm not sure how to init this so that both the sprite nodes appear. Effectively what I want is the character body on top, and the health bar underneath it.
I know this is the init method for SKSpriteNode to initialize with a size and texture:
override init() {
let texture = SKTexture(imageNamed: "CharacterStanding.png")
super.init(texture: texture, color: UIColor.clearColor(), size: texture.size())
}
does SKNode have a similar init method? or is it sufficient to init the two SKSpriteNodes within the SKNode and then just make a new GameCharacter object in the game scene?
or I guess what I'm trying to ask is; how do I init this GameCharacter(SKNode) object and both character and health bar appear?
Upvotes: 1
Views: 584
Reputation: 119
If you're having problems with z-index in sprite kit its generally caused by the optimisations the system make with ignoresSiblingOrder = true
, you can handle this defining zPosition by hand in each node.
More information about Drawing Order for a Node Tree
Upvotes: 1
Reputation: 10021
heres a solution I found so far:
in the init method just do the following:
override init() {
super.init()
self.addChild(characterBody)
characterHealthBar.position =
CGPoint(x: characterBody.position.x, y: characterBody.position.y - 45)
self.addChild(characterHealthBar)
}
and this will init the node with the character and the health bar below him/her
Upvotes: 1