如何让任何模拟器/设备的SKSpriteNode位置相同?

时间:2016-06-01 19:25:29

标签: xcode sprite-kit xcode7 skspritenode

在我的游戏中,当我在虚拟模拟器上运行应用程序而不是真实设备(我的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
}
}

如何使每个模拟器/物理设备的节点位置相同?

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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。这对我有用。