SpriteKit Sprites Not Scaling Properly

眉间皱痕 提交于 2019-12-11 04:25:27

问题


The sprites in my SpriteKit game, though properly scaled, are being rendered extremely small in my scenes. I've been following a few different tutorials using similarly sized sprites and theirs are scaled fine in the simulator. Below are some code snippets and screenshots. For example, the pause button on the top right of the screen is crazy small, yet the actual resolutions of the assets are standard in size.

GameScene.swift

private let pauseTex = SKTexture(imageNamed: "PauseButton")    

...

private func setupPause() {
    pauseBtn.texture = pauseTex
    pauseBtn.size = pauseTex.size()
    pauseBtn.anchorPoint = CGPoint(x: 1.0, y: 1.0)
    pauseBtn.position = CGPoint(x: size.width, y: size.height)
    pauseBtn.zPosition = Layer.Foreground.rawValue
    worldNode.addChild(pauseBtn)Btn)

    ...
}

GameVC.swift

override func viewWillLayoutSubviews() {
    super.viewWillLayoutSubviews()

    ...

    if let skView = self.view as? SKView {
        if skView.scene == nil {

            let aspectRatio = view.bounds.size.height / view.bounds.size.width
            let scene = MenuScene(size: CGSize(width: 750, height: 750 * aspectRatio))

            scene.scaleMode = .aspectFill
            skView.ignoresSiblingOrder = true

            ...
        }
    }
}


回答1:


Basically what has happened here is Mike got confused with scene size vs screen size. He was thinking that since the sprite was 32 pixels, it should be taking up 10% of the iPhone SE Screen since it has a 1x width of 320 (Natively it is 640 [2x]) But in reality, his scene is 750 pixels wide, so it was only showing at 5%. He switched his scene size to be 375x667 (iPhone 6 non retina resolution) to properly use the retina graphics, and now everything should be aligning up for him.

The only issue he is going to come across with now, is cropping on iPad devices. He just needs to work in safe zones for this.




回答2:


The size of your assets will not always reflect the desired size on-screen due to resolution discrepancies between different devices (this is true if the SKScene's scaleMode is set to resizeFill). If you want a specific size for your sprite, you need to set the SKNode's size property to your desired value.

What this would look like in your method:

private func setupPause() {

    pauseBtn.anchorPoint = CGPoint(x: 1.0, y: 1.0)

    // Set size property (arbitrarily selected below)
    pauseBtn.size = CGSize(width: 50, height: 50)

    pauseBtn.position = CGPoint(x: size.width, y: size.height)
    pauseBtn.zPosition = Layer.Foreground.rawValue
    worldNode.addChild(pauseBtn)

}


来源:https://stackoverflow.com/questions/41002604/spritekit-sprites-not-scaling-properly

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!