Equivalent of Skaction Scaletox for a Given Duration in Unity

Run SKAction x number of times

You can't use the for loop for this, you need to use SKAction.repeat. The reason being that the loop will execute self.run(SKAction.sequence([wait,action])) 50 times without waiting for SKAction.sequence([wait,action]) to finish. Where as I assume what you really wanted was to execute self.run once and SKAction.sequence([wait,action]) 50 times one after the other.

Here is an example I have made for you.

    let wait = SKAction.wait(forDuration:0.1)
let action = SKAction.run {
time = time - 1
}
let repeatAction = SKAction.repeat(SKAction.sequence([wait,action]), count: 50)

self.run(repeatAction)

How to increase (animate) the width of the square on both ends?

What you want to do is use SKAction.scaleXTo to achieve what you are looking for:

SKAction.scaleXTo(sceneWidth / spriteWidth, duration: 1).  

Now if you want the left and right side to not scale evenly, but instead reach both edges at the same time, what you can do is change the anchor point.

The math behind this assumes that your original anchor point is (0.5,0.5)

sprite.anchorPoint = CGPointMake(sprite.position.x / scene.width,sprite.anchorPoint.y)

E.G. Scene size width is 100, sprite is at x 75

What this is basically saying is that your sprite is at some percentage of the scene, in case of the example, 75%. so by changing the anchor point to .75, what is going to happen is the left side will fill faster than the right side when you are expanding your width since the left side of the anchor point has 75% of the width, and the right side has 25% of the width .

Lets say we set the scale to 2, that means the left side of the anchor point will now be at 150%, while the right side will be at 50%.

How to scale a object using SKAction with repeat forever in Sprite Kit

Use + scaleXBy:y:duration::

let increaseSize = SKAction.scaleXBy(1, y: CGFloat(1.5), duration: 0.5)... // the rest of your code (no need to change it)

The scaleYTo action will scale the node to a y size of 1.5 once, and then continue infinitely scaling it from 1.5 to 1.5 its original size, which does nothing at all. The scaleXby:y:duration: action will increase the relative scale, and so continue scaling it forever.



Related Topics



Leave a reply



Submit