Why won't my SKSpriteNodes appear in the scene?

坚强是说给别人听的谎言 提交于 2019-12-23 01:03:28

问题


I am trying to add a couple of nodes to the scene. When I run the app I just have the default gray background. Neither of the nodes appear in the scene. Here is what I have:

class GameScene: SKScene {
    override func didMoveToView(view: SKView) {
        /* Setup your scene here */

        let jumper = SKSpriteNode(imageNamed:"Jumper")
        jumper.xScale = 0.25
        jumper.yScale = 0.25
        jumper.position = CGPointMake(50, 300)

        self.addChild(jumper)

        let blob = SKSpriteNode(imageNamed:"Blob")
        blob.position = CGPointMake(160, 30)
        blob.xScale = 0.5
        blob.yScale = 0.25

        self.addChild(blob)
    }

I have checked the image titles and everything is correct there.


回答1:


The Problem

Your sprites are not visible because your scene is not completely within the simulator's window. By default the scene's size is 1024 x 768 (specified by GameScene.sks), while the simulator is displaying only 375 x 667 points (if you selected the iPhone 6/6s simulator). Unless the scene's size is set correctly, the bottom-left corner of the simulator's window will not be the (0,0) position of your scene.

To correct this issue,

Method 1:

Change the scene's size property in GameScene.sks to match the size of the view. I suggest you set the scene size to 375 x 667 (iPhone 6/6s). If you do this and set the scene's scaleMode property to .AspectFill (the default value), SpriteKit will automatically scale the scene to fit in the view of the iPhone 6+, 6s+, 5, and 5s. This will save you time when porting your game to these devices.

Method 2: In your view controller, replace this

scene.scaleMode = .AspectFill

with this

scene.scaleMode = .ResizeFill

Warning: Using this method will work but may require additional effort to port your game to other iOS devices with a different screen size.



来源:https://stackoverflow.com/questions/25233506/why-wont-my-skspritenodes-appear-in-the-scene

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