Calibrate and scale SKSpriteNode

There are several game objects in my iOS game, some of which have higher resolution than others. The graphics used for the game object are randomly selected at runtime. I would like to make sure that they all do not use a specific size when used, so I developed the following algorithm:

while self.spriteNode.rSize.width > 100 && self.spriteNode.rSize.height > 100 { self.xScale -= 0.01 self.yScale -= 0.01 } 

where spriteNode is an object whose texture is graphic and rSize is an extended computed object on SKSpriteNode, which returns the size of the accumulated frame from node.

Often this leads to an endless loop. What is the problem?

UPDATE 1

Based on the LearnCocos2D comment, I tried the following:

 let reqXScale = 50/self.spriteNode.rSize.width let reqYScale = 50/self.spriteNode.rSize.height self.xScale = reqXScale self.yScale = reqYScale 

Although this solves the infinite loop problem, some objects are compressed rather than preserving the original aspect ratio.

Also, here is the code defining rSize:

 var rSize: CGSize { return self.calculateAccumulatedFrame().size } 

I have used this many times before.

+5
source share
1 answer

I was able to figure it out. I use random width because this is what I need.

 // Scaling let aspectRatio = self.spriteNode.rSize.width/self.spriteNode.rSize.height let randWidth = CGFloat(rand(60, 90)) self.spriteNode.size = CGSize(width: randWidth, height: randWidth/aspectRatio) 
+4
source

Source: https://habr.com/ru/post/1207511/


All Articles