0
votes

How would I make an SKShapeNode scale at randomized sizes forever while not exceeding a maximum set size and not smaller than a minimum set size?

1
Hi and welcome to SO. You'll need to give us some more detail - how often do you want its scale factor to change and do you want it to change size instantly or to transition to the new size over some period of time? How much experience do you have in SK, Swift, games programming or programming in general? - Steve Ives
Hi, I am just getting started with SK and Swift. I dig it. I’m making a simple game and this is the main functionality I need to solve. I know how to scale the shape over time but can’t figure out how to make the shape scale to different sizes every 0.5 seconds while not exceeding the max and minimum size set. So this would be random sizes that are never the same not a sequence. Thanks for helping let me know if you need more details! - chrismlyle
Chris - I've posted an answer that may help. Have a look and see what you think. I've had to guess at some of your requirements - Steve Ives
Ahhh smart!! I think that is exactly what I am after. I am having trouble getting it to work still though. I will keep at it and report back. Thanks for the help! - chrismlyle
I wrote this pretty much from memory, cannibalising my other answers and testing the randomising code in the iPad Playgrounds app, so there may well be an error or two. Feel free to post any issues and I'll have a look. - Steve Ives

1 Answers

0
votes

One way to do this (if I understand your question correctly), would be have 3 properties of type TimeInterval that track how long it has been since the sprite was last scaled, how often the sprite should be scaled (which will initialise as 0.5s) and how long the scale action takes (default is the same as the time between scales):

  var timeOfLastScale: CFTimeInterval = 0.0
  var timePerScale: CFTimeInterval = 0.5
  var scaleTime = timePerScale 

We initialise the time since the last scale to 0, as it hasn't happened yet. I also use the timePerScale as the duration of the scale effect, so as soon as it stops scaling, a new scale action starts i.e. the node is constantly scaling. These can be modified in code for different effects.

We also need 2 properties that define the maximum and minimum scale sizes (from 0-100%) and a computed property of the overall scaling range (we make this a computed property so that if you change the max or min scale factors in your code, you don't have to re-calculate scaleRange):

  var maxScale; UInt32 = 100
  var minScale:UInt32 = 25
  var scaleRange: Uint32 {
      get {return maxScale - minScale}
      }

(I'm assuming that the node can scale between 25% and 100% of it's normal size)

In update: call a function that checks to see if the scale time interval has passed. If it has, then create and run a new SKAction to scale the node and reset to time since the last scale.

If the scale time hasn't passed yet, do nothing:

override func update(_ currentTime: TimeInterval) {
   scaleNode(currentTime)
   // Rest of update code
   }

func scaleNode(_ currentTime: CFTimeInterval) {
   timeSinceLastScale = currentTime - timeOfLastScale

   if timeSinceLastScale < timePerScale {return}

   // Time per scale has passed, so calculate a new scale actiona and re-scale the node...
   let scaleFactor = Float(arc4random_uniform(scaleRange)+minScale)/100
   let scaleAction = SKAction.scale(to: scaleFactor,duration: scaleTime)
   nodeToBeScaled.run(scaleAction)   
   timeOfLastScoreDecrement = currentTime
}