1

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.

  • Define "better looking". – Eric Aya Jun 19 '16 at 19:15
  • I want it to have a border. Like in this tutorial by Ray Wenderlich. However he is using Objective-C https://cdn3.raywenderlich.com/wp-content/uploads/2013/12/ship_colliding_losing_points-480x270.png @EricD – Dean Sponholz Jun 19 '16 at 19:20
  • @JoshCaswell How do i programmatically add a border around the health bar i currently have? – Dean Sponholz Jun 19 '16 at 19:24
  • possible duplicate of [Can I add a border to an SKSpriteNode, similar to UIView?](http://stackoverflow.com/q/20889222) – jscs Jun 19 '16 at 19:41
  • That ray wenderlich tutorial is in swift as well, you just need to search for it. Check my answer I posted with a link at the bottom. – crashoverride777 Jun 19 '16 at 20:27

1 Answers1

1

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

crashoverride777
  • 10,581
  • 2
  • 32
  • 56