Sizing and scaling SKSpriteNode

柔情痞子 提交于 2019-12-07 04:18:47

问题


I have multiple game objects in my iOS game, some of which have a greater resolution than others. The graphics to use for the game object is chosen randomly at runtime. I'd like to make sure that they all don't go over a certain size when used, so I devised 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 the object whose texture is the graphic and rSize is an extended computed property on SKSpriteNode that returns the size of the accumulated frame of the node.

Often, this results in an infinite loop. What's the problem?

UPDATE 1

Based on LearnCocos2D's comment, I have tried the following:

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

Though this solves the infinite loop issue, some objects are squished rather than keeping their original aspect ratio.

Also, here's the code that defines rSize:

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

I have used this reliably multiple times before.


回答1:


I was able to figure it out. I am using a random width because that is what I require.

// 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)


来源:https://stackoverflow.com/questions/27087246/sizing-and-scaling-skspritenode

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!