解释
我正在尝试让我的游戏在 iPhone 和 iPad 上运行。我创建了这个测试项目,但是 Sprite 尺寸只适用于 iPhone 6s Plus;在所有其他设备中, Sprite 位置不佳(如您在下面的屏幕截图中所见)。
除了使用 setScale
根据设备尺寸缩小 Sprite (我认为这不是正确的方法)之外,我还能做些什么来使其通用吗?
代码
您可以下载项目here .
GameViewController.swift
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
// Set view size.
let scene = GameScene(size: view.bounds.size)
// Configure the view.
let skView = view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true
/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true
/* Set the scale mode to scale to fit the window */
scene.scaleMode = .ResizeFill
skView.presentScene(scene)
}
}
GameScene.swift
import SpriteKit
class GameScene: SKScene {
var bg = SKSpriteNode()
var square = SKSpriteNode()
var xIcon = SKSpriteNode()
func addBG(){
bg = SKSpriteNode(imageNamed: "bg")
bg.size = (self.view?.bounds.size)!
bg.anchorPoint = CGPointZero
bg.position = CGPointZero
bg.zPosition = 0
addChild(bg)
}
func addSquare(){
square = SKSpriteNode(imageNamed: "square")
square.anchorPoint = CGPointZero
square.position = CGPoint(x: self.frame.width / 2 - square.frame.width / 2, y: self.frame.height / 2 - square.frame.height / 2)
square.zPosition = 1
addChild(square)
}
func addxIcon(){
xIcon = SKSpriteNode(imageNamed: "xIcon")
xIcon.anchorPoint = CGPointZero
xIcon.position = CGPoint(x: self.frame.width / (4/3) - xIcon.frame.width / 2, y: self.frame.height / (16/13) - xIcon.frame.height / 2)
xIcon.zPosition = 2
addChild(xIcon)
}
override func didMoveToView(view: SKView) {
/* Setup your scene here */
addBG()
addSquare()
addxIcon()
}
}
提前致谢,
路易斯。
最佳答案
可以通过如下方式获取屏幕的宽度:
let screenSize: CGRect = UIScreen.mainScreen().bounds
let screenWidth = screenSize.width
然后设置元素相对于screenWidth
的大小。
以防万一您还没有弄清楚,1x、2x 和 3x 与屏幕上图形的大小没有任何关系。它们只与决议有关。
关于ios - 通用 Sprite 尺寸,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38619652/