0

In my game, the position of my SKNodes slightly change when I run the App on a virtual simulator vs on a real device(my iPad).

Here are pictures of what I am talking about.

This is the virtual simulator

This is my Ipad

It is hard to see, but the two red boxes are slightly higher on my iPad than in the simulator

Here is how i declare the size and position of the red boxes and green net: The following code is located in my GameScene.swift file

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

Additionally my GameViewController.swift looks like this

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
}
}

How can I make the positions of my nodes be the same for each simulator/physical device?

  • @Darvydas this question is very similar to the once you answered in this stack overflow post "http://stackoverflow.com/questions/33787770/how-to-scale-position-nodes-swift-spritekit-custom-view" Could you point me in the right direction please? – Dean Sponholz Jun 01 '16 at 19:27
  • Maybe this problem has to do with the way I am setting the background? – Dean Sponholz Jun 02 '16 at 00:30

2 Answers2

0

You should round those floating point values to integers via a call to (int)round(float) so that the values snap to whole pixels. Any place where you use CGPoint or CGSize should use whole pixels as opposed to floating point values.

MoDJ
  • 4,309
  • 2
  • 30
  • 65
  • If I use an integer value instead of a float won't the positioning of the Node not be as exact as I need? Also I am not sure exactly how to implement this in my code. Is there a way to round the floating point value in same line that I am declaring the position? @MoDJ – Dean Sponholz Jun 01 '16 at 19:43
  • Please take some time to understand pixels and how points map to pixels in iOS and SpriteKit: http://math.hws.edu/graphicsbook/c2/s1.html – MoDJ Jun 01 '16 at 19:48
  • After taking some time to understand that I think I need to implement anchor points for each SKSpriteNode @MoDJ – Dean Sponholz Jun 01 '16 at 22:31
  • This does not fix the problem. If i use integers as parameters for CGPoint(40, 40) the SKNode is still not at the same position on my iPad vs simulator – Dean Sponholz Jun 02 '16 at 17:53
0

If you are making a Universal application you need to declare the size of the scene using integer values. Here is an example:

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

Then when you initialize the positions and sizes of your nodes using CGPoint and CGSize, make them dependant on SKScene size. Here is an example:

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

If you declare the size of the scene for a Universal App like this:

scene.size = skView.bounds.size

then your SKSpriteNode positions will be all messed up. You may also need to change the scaleMode to .ResizeFill. This worked for me.