Dean Sponholz
Dean Sponholz

Reputation: 91

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))
    dragonNode.addChild(healthbar)
    return dragonNode

}

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

Upvotes: 1

Views: 1666

Answers (1)

crashoverride777
crashoverride777

Reputation: 10674

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

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

https://www.raywenderlich.com/90520/trigonometry-games-sprite-kit-swift-tutorial-part-1

Hope this helps

Upvotes: 1

Related Questions