如何使任何 simulator/device 的 SKSpriteNode 位置相同?

How can I make SKSpriteNode positions the same for any simulator/device?

在我的游戏中,当我 运行 应用程序在虚拟模拟器与真实设备(我的 iPad)上时,我的 SKNodes 的位置略有变化。

这是我正在谈论的图片。

This is the virtual simulator

This is my Ipad

很难看,但是两个红框在我的iPad上比在模拟器上稍微高一些

以下是我如何声明红框和绿网的大小和位置: 以下代码位于我的GameScene.swift 文件

func loadAppearance_Rim1() {                                                
Rim1 = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake((frame.size.width) / 40, (frame.size.width) / 40))
Rim1.position = CGPointMake(((frame.size.width) / 2.23), ((frame.size.height) / 1.33))          
Rim1.zPosition = 1                                                      
addChild(Rim1)                                                          
}

func loadAppearance_Rim2(){                                                
    Rim2 = SKSpriteNode(color: UIColor.redColor(), size: CGSizeMake((frame.size.width) / 40, (frame.size.width) / 40))
    Rim2.position = CGPoint(x:  ((frame.size.width) / 1.8), y: ((frame.size.height) / 1.33))                                                                                                    
    Rim2.zPosition = 1                                                      
    addChild(Rim2)                                                        
}
func loadAppearance_RimNet(){                                        
    RimNet = SKSpriteNode(color: UIColor.greenColor(), size: CGSizeMake((frame.size.width) / 7.5, (frame.size.width) / 150))
    RimNet.position = CGPointMake(frame.size.width / 1.99, frame.size.height / 1.33)
    RimNet.zPosition = 1                                                    
    addChild(RimNet)                                                        
}
func addBackground(){
    //background
    background = SKSpriteNode(imageNamed: "Background")
    background.zPosition = 0
    background.size = self.frame.size
    background.position = CGPoint(x: self.size.width / 2, y: self.size.height / 2)
    self.addChild(background)
}

另外我的 GameViewController.swift 看起来像这样

import UIKit
import SpriteKit

class GameViewController: UIViewController {

var scene: GameScene!

override func viewDidLoad() {
    super.viewDidLoad()

    //Configure the view
    let skView = view as! SKView
    //If finger is on iphone, you cant tap again
    skView.multipleTouchEnabled = false

    //Create and configure the scene
    //create scene within size of skview
    scene = GameScene(size: skView.bounds.size)
    scene.scaleMode = .AspectFill
    scene.size = skView.bounds.size
    //scene.anchorPoint = CGPointZero

    //present the scene
    skView.presentScene(scene)



}

override func shouldAutorotate() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return .Landscape
    } else {
        return .All
    }
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Release any cached data, images, etc that aren't in use.
}

override func prefersStatusBarHidden() -> Bool {
    return true
}
}

如何使每个 simulator/physical 设备的节点位置相同?

您应该通过调用 (int)round(float) 将这些浮点值四舍五入为整数,以便这些值捕捉到整个像素。任何使用 CGPoint 或 CGSize 的地方都应该使用整像素而不是浮点值。

如果您正在制作通用应用程序,则需要使用整数值声明场景的大小。这是一个例子:

scene = GameScene(size:CGSize(width: 2048, height: 1536))

然后当您使用 CGPoint 和 CGSize 初始化节点的位置和大小时,使它们依赖于 SKScene 大小。这是一个例子:

node.position = CGPointMake(self.frame.size.width / 2, self.frame.size.height / 2)

如果您像这样声明通用应用程序的场景大小:

scene.size = skView.bounds.size

那你的SKSpriteNode位置就全乱了。您可能还需要将 scaleMode 更改为 .ResizeFill。这对我有用。