Reputation: 434
This is my current code for the progress bar. In the GameScene I have set the var xProgress = 1. Shouldn't the empty image appear as the value should now = 0?
I'm trying to test the SKNode before making it the value decrease over time so it reduces slowly.
class IMProgressBar : SKNode{
var emptySprite : SKSpriteNode? = nil
var progressBar : SKCropNode
init(emptyImageName: String!,filledImageName : String)
{
progressBar = SKCropNode()
super.init()
let filledImage = SKSpriteNode(imageNamed: filledImageName)
progressBar.addChild(filledImage)
progressBar.maskNode = SKSpriteNode(color: UIColor.white,
size: CGSize(width: filledImage.size.width * 2, height: filledImage.size.height * 2))
progressBar.maskNode?.position = CGPoint(x: -filledImage.size.width / 2,y: -filledImage.size.height / 2)
progressBar.zPosition = 0.1
self.addChild(progressBar)
if emptyImageName != nil{
emptySprite = SKSpriteNode.init(imageNamed: emptyImageName)
self.addChild(emptySprite!)
}
}
func setXProgress(xProgress : CGFloat){
var value = xProgress
if xProgress < 0.1{
value = 0
}
if xProgress > 0.1 {
value = 0
}
progressBar.maskNode?.xScale = value
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
}
In my GameScene I also have:
let progressBar = IMProgressBar(emptyImageName: nil,filledImageName: "health")
progressBar.position = CGPoint(x: self.frame.midX, y: self.frame.height / 3)
self.addChild(progressBar)
Upvotes: 4
Views: 1395
Reputation: 12773
You can implement a progress bar using two sprite nodes, where one node is the bar and the other is the background.
First, create the bar and background nodes with the same size:
background = SKSpriteNode(color:SKColor.white, size:size)
bar = SKSpriteNode(color:color, size:size)
Next, set the bar's zPosition
to a value higher than the background, so the bar will appear above the background. Left-justify the bar by setting the anchorPoint
to (0, 0.5)
and set the position of the bar so it's at the left edge of the background.
bar.zPosition = 1.0
bar.anchorPoint = CGPoint(x:0.0,y:0.5)
bar.position = CGPoint(x:-size.width/2,y:0)
Finally, set the progress amount by scaling the bar in the x direction by
bar.xScale = value
where value
is a value between 0 and 1, inclusive.
Here's the full implementation as a subclass of SKNode
:
class ProgressBar:SKNode {
var background:SKSpriteNode?
var bar:SKSpriteNode?
var _progress:CGFloat = 0
var progress:CGFloat {
get {
return _progress
}
set {
let value = max(min(newValue,1.0),0.0)
if let bar = bar {
bar.xScale = value
_progress = value
}
}
}
convenience init(color:SKColor, size:CGSize) {
self.init()
background = SKSpriteNode(color:SKColor.white,size:size)
bar = SKSpriteNode(color:color,size:size)
if let bar = bar, let background = background {
bar.xScale = 0.0
bar.zPosition = 1.0
bar.position = CGPoint(x:-size.width/2,y:0)
bar.anchorPoint = CGPoint(x:0.0,y:0.5)
addChild(background)
addChild(bar)
}
}
}
usage:
let progressBar = ProgressBar(SKColor.blue, size:CGSize(width:100, height:10))
addChild(progressBar)
progressBar.progress = 0.5
Upvotes: 6