Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpriteKit - Scale node to X from right to left

I have a SKNode that is a load bar. This bar is supposed to shrink from right to left. For doing this I use the following code:

let loadBarNode = self.childNodeWithName("loadBar")

    if loadBarNode != nil{

       let countdown = SKAction.scaleXTo(0, duration: 3)
        loadBarNode?.runAction(countdown)

    }

When I run this code, this happens:

What happens when code runs.

I would like to know how to shrink the shape from right to left, like this:

It's supposed to look like this.

Any help is appreciated!

like image 483
Omar Dlhz Avatar asked Apr 06 '15 19:04

Omar Dlhz


2 Answers

I believe a simpler solution is to change the anchor of your node.

loadBarNode.anchorPoint = CGPointMake(0.0, 0.5)

Scaling is based on the anchorPoint of your node and this would cause it to expand to the right.

like image 170
Skyler Lauren Avatar answered Oct 04 '22 06:10

Skyler Lauren


Make an SKAction sequence that has your countdown action, plus an SKAction which moves your X.

class func moveToX(_ x: CGFloat, duration sec: NSTimeInterval) -> SKAction

So as your load bar shrinks, you want to offset your X by half the shrunk distance.

like image 31
maelswarm Avatar answered Oct 04 '22 07:10

maelswarm