调整和缩放 SKSpriteNode [英] Sizing and scaling SKSpriteNode

查看:104
本文介绍了调整和缩放 SKSpriteNode的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 iOS 游戏中有多个游戏对象,其中一些具有比其他更高的分辨率.用于游戏对象的图形在运行时随机选择.我想确保它们在使用时都不会超过一定的大小,因此我设计了以下算法:

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
}

其中 spriteNode 是纹理为图形的对象,rSize 是 SKSpriteNode 上的扩展计算属性,返回节点累积帧的大小.

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?

更新 1

根据 LearnCocos2D 的评论,我尝试了以下操作:

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.

此外,这里是定义 rSize 的代码:

Also, here's the code that defines rSize:

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

我以前多次可靠地使用过这个.

I have used this reliably multiple times before.

推荐答案

我能够弄清楚.我使用的是随机宽度,因为这是我所需要的.

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)

这篇关于调整和缩放 SKSpriteNode的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆