2016-07-08 45 views
0

iPhone 6和6s应具有750 x 1334分辨率[1],并且iPhone 5的屏幕比例为16:9 [2]。因此,为了获得适合完美应用的背景图像,它必须具有16:9的比例。我正在使用SpriteKit开展一个项目,我希望游戏有一个能够覆盖背面的壁纸。但是,当我在模拟器上运行应用程序时,背景图像总是会在右侧和左侧裁剪。我甚至尝试过各种比例和分辨率。此项目背景的代码是:将背景图像添加到SpriteKit项目

let background = SKSpriteNode(imageNamed: "backtImage") 
    background.size = self.size 
    background.position = CGPoint(x: self.size.width/2, y: self.size.height/2) 
    background.zPosition = 0 
    self.addChild(background) 

我在做什么错?

+0

在代码中,您发布了self.size中的self。它是SKScene吗? –

+0

@MrFlynn是的,这是SKScene – Gabe12

回答

0

发生这种情况可能是因为您的SKView与您的SKScene尺寸不符。

来源:

@available(iOS 7.0, *) 
public enum SKSceneScaleMode : Int { 

    case Fill /* Scale the SKScene to fill the entire SKView. */ 
    case AspectFill /* Scale the SKScene to fill the SKView while preserving the scene's aspect ratio. Some cropping may occur if the view has a different aspect ratio. */ 
    case AspectFit /* Scale the SKScene to fit within the SKView while preserving the scene's aspect ratio. Some letterboxing may occur if the view has a different aspect ratio. */ 
    case ResizeFill /* Modify the SKScene's actual size to exactly match the SKView. */ 
} 

您可以如下更改代码:

override func viewDidLoad() { 
     super.viewDidLoad() 

     if let scene = GameScene(fileNamed:"GameScene") { 
      // Configure the view. 
      let skView = self.view as! SKView 
      ... 
      /* Set the scale mode to scale to fit the window */ 
      scene.scaleMode = .ResizeFill // <- this is an example 

      skView.presentScene(scene) 
     } 
    } 
+0

不,不应该永远不要将你的场景大小设置为你的SKView大小,resizefill为你做这件事 – Knight0fDragon

+0

是的,你有理由,它也可以混淆OP,我删除它,谢谢你Knight0fDragon。 –

+0

NP,我可以看到设置一个场景大小到视图的唯一场景就是OSX的情况,我们有一个可以调整大小的窗口,并且出于某种原因,您希望保留与您启动应用的场景长宽比 – Knight0fDragon

0

如果你的形象是越来越裁剪左侧和右侧(纵向)然后,我们正在处理.AspectFill缩放模式,并且您的场景比屏幕分辨率更短。这可能是因为模板构建中提供的默认场景大小为正方形。进入你的.sks文件,并将场景大小更改为9x16的比例,然后解决问题

+0

但是这个改变折衷了iPhone 4s的结果。 OP也可以选择iPhone项目,不是通用的,但你也必须考虑4s设备,仍然可用。 –

+0

从这篇文章中,OP不关心iphone 4,所以我的猜测是丢失图像的顶部和底部是可以接受的 – Knight0fDragon

+0

我个人更喜欢用代码做这些改变。 –