Dean Sponholz Dean Sponholz - 1 year ago 212
Swift Question

How to add a border to a health bar in SpriteKit?

I have a health bar I created in SpriteKit.

I have a function that creates the health bar and attaches it to an SKSpriteNode(dragonNode)

The health bar I current have implemented is simply a SKSpriteNode with a rectangular shape.

Here is the code

func dragonHealth(dragonNode: SKSpriteNode) -> SKSpriteNode{

healthbar = SKSpriteNode(color: UIColor.greenColor(), size: CGSizeMake(self.frame.size.width * 0.5, self.frame.size.height * 0.015))
healthbar.anchorPoint = CGPointMake(0, 0.5)
healthbar.position = CGPointMake(self.frame.size.width * -0.23, self.frame.size.height * 0.3)
healthbar.runAction(SKAction.resizeToWidth(self.frame.size.width * 0.1, duration: 3))
return dragonNode


Can someone share a way to implement a border to my current health bar. I think it will look better that way.


Why don't you simply add another sprite underneath your healthbar sprite that is slightly bigger and in a different colour. (Make it a child or parent of your main healthbar so they stay/move together).

You could also use SKShapeNodes which give you the ability to set border colours.

healthBar = SKShapeNode(
healthBar.lineWidth = ...
healthBar.strokeColor = ...

There is also this tutorial on Ray Wenderlich where they are using health bars, its about half way through the page.

Hope this helps