How to scale/position nodes Swift SpriteKit? Custom View?

前端 未结 1 469
孤城傲影
孤城傲影 2021-01-19 14:32

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

1条回答
  •  不思量自难忘°
    2021-01-19 15:14

    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?

    0 讨论(0)
提交回复
热议问题