iOS Universal Device App with SpriteKit, how to scale nodes for all views?

后端 未结 1 997
说谎
说谎 2020-11-27 18:24

I want to make a landscape app to be universal, so that the sprite nodes scale proportionally to whatever view size is running the

相关标签:
1条回答
  • 2020-11-27 18:59

    I initially tried to do the scaling myself with 2 games and it was just madness (scene size = view size or scene scale mode = .ResizeFill). You have to adjust all values e.g font size, sprite sizes, impulses etc for all devices and it will never be consistent.

    So you have 2 options basically

    1) Set scene size to 1024X768 (landscape) or 768x1024 (portrait). This was the default setting in Xcode 7.

    You than usually just have/show some extra background at the top/bottom (landscape) or left/right (portrait) on iPads which gets cropped on iPhones.

    Examples of games that show more on iPads / crop on iPhones:

    Altos Adventure, Leos Fortune, Limbo, The Line Zen, Modern Combat 5.

    2) Apple changed the default scene size in xCode 8 to iPhone 6/7 (750*1334-Portait, 1337*750-Landscape). This setting will crop your game on iPads.

    Examples of games that show less on iPads:

    Lumino City, Robot Unicorn Attack

    Chosing between the 2 options is up to you and depends what game you are making. I usually prefer to use option 1 and show more background on iPads.

    Regardless of scene size scale mode is usually best left at the default setting of .aspectFill.

    To adjust specific things such as labels etc you can do it this way

     if UIDevice.current.userInterfaceIdiom == .pad {
       ...
     }
    

    You can try the scene scaling yourself, create a new SpriteKit sample game project. Run on all iPhones and you will notice the HelloWorld label looks perfect on all devices.

    Now change the default settings to scene size = frame or use .ResizeFill, the HelloWorld label is not scaled properly anymore on all devices.

    As a side note, the line

     if let scene = GameScene(fileNamed: "GameScene")
    

    references the GameScene.sks file. You said you do everything programatically, therefore you can probably delete the GameScene.sks file and change the line to

     let skView = view as! SKView!
     let scene = GameScene(size: CGSize(width: 1024, height: 768)) // 768 x 1024 if portrait
    

    Update:

    I am now using a slightly different variant as I had problems adapting my game to iPhoneX. I set scene size to 1334x750 and use aspect fit as scale mode. I than run some code to remove the black bars if need eg iPad or iPhone X as I prefer to show more background. It’s based on this great article.

    http://endlesswavesoftware.com/blog/spritekit-skscene-scalemode/

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