5

I'm working on a game but can't figure out the right way to scale/ position everything. I have a universal app and when I switch from device to device the nodes aren't in the right place everytime. I'm using AspectFill because it sizes my nodes the the right way for each device. The problem is the positioning. I don't know if I'm right in making the conclusion that I need to figure out how to make the view the same size as the screen so that it changes for each device and then positions and scales everything properly, but this is what I've been trying to figure out. I've been trying many different things online and looking at things like how to make a custom view. I've tried to make a rectangle that ha constraints to fill the screen and then setting that rectangle to equal the UIView or SKView. I've tried many different things and looked at many things online but they are either confusing and I don't know if I'm trying them right or they don't pertain to my situation. This is causing a lot of confusion for me and I was hoping someone could help with this issue. I think the issue is that I need to make a custom view that relates to the size of the screen. I don't know if this is possible or what I should be going after. It would be great if someone could clear this up for me.

1 Answers1

4

To keep my games universal I make all my nodes sizes and positions dependant on SKScene size

Instead of using hardcoded numbers try using proportions from your SKScene size. For example: Instead of writing this:

node.position = CGPointMake(100, 100)

Write somthing like this:

node.position = CGPointMake(world.frame.size.width / 32 * 16, world.frame.size.height / 18 * 9)

world - SKNode of exactly same size as your SKScene

Same for sizes

let blackSquare = SKSpriteNode(texture: nil, color: UIColor.blackColor(), size: CGSizeMake(world.frame.size.width / 32, world.frame.size.width / 32))

Also GameViewController should look something like this:

override func viewDidLoad() {
    super.viewDidLoad()

    // Configure the view

    let skView = view as! SKView
    skView.multipleTouchEnabled = false

    // Create and configure the scene

    let scene = GameScene(size: skView.bounds.size)
    scene.scaleMode = .AspectFill
    scene.size = skView.bounds.size

    skView.presentScene(scene);
}

Was this helpfull?

Darvas
  • 974
  • 3
  • 14
  • 27
  • Do you import the picture files for your nodes in 1x 2x and 3x sizes? – Stevie Thiel Nov 19 '15 at 15:19
  • No i do not. But try to keep your images as lighter as you can but with highest resolution – Darvas Nov 19 '15 at 15:52
  • Ok thanks, and also how do you make a your world node the same size as the SKView and where do you define it? – Stevie Thiel Nov 19 '15 at 17:57
  • @StevieThiel in your scene `var world = self`. If you creating node in your Scene you just can use self (just replace `world` with `self`). – Darvas Nov 19 '15 at 18:07
  • Okay I did this and the point of the app is for 3 nodes to move up the screen. One on the left, on in middle, and one on right. It works for the iPad and is positioned perfectly in the simulator. I divide self.frame.size.width/3 for the node on the left and divide by 2 for the one in the middle. However, when I use it on the iPhone it's positioned too far to the left. I don't know why this happens? – Stevie Thiel Nov 19 '15 at 18:19
  • @StevieThiel Can you show all your nodes positions ? It shoudl be something like this `node1.position = CGPointMake(frame.size.width / 5, frame.size.height / 2)` `node2.position = CGPointMake(frame.size.width / 5 * 2.5, frame.size.height / 2) ` `node3.position = CGPointMake(frame.size.width / 5 * 4, frame.size.height / 2) ` – Darvas Nov 19 '15 at 18:31
  • Pretty sure it's all working now thanks for your help! – Stevie Thiel Nov 19 '15 at 20:53
  • @StevieThiel If i answered your question you shuld my mark my answer. – Darvas Nov 20 '15 at 06:54