我的精灵节点不会出现在我的场景中

时间:2019-06-14 21:08:20

标签: ios swift sprite-kit

我创建了一个SKScene并使用addChild将我的SKSpriteNode放置在场景中。它指示该节点在右下角存在,但没有精灵可见。一切看起来都不错,但没有结果。我的scene.scaleMode是.aspectFill,我确保SKSpriteNode(imageNamed:“ GreenBlock”)是正确的。我已经看了很多遍了,但是对我来说却没有意义,为什么它不会显示在屏幕上……

这是一个简单的任务。我见过其他人做的完全一样,而且行得通。我已经尝试了所有我能想到的...

我的视图控制器

import UIKit
import SpriteKit

class GreenSnakeController: UIViewController {

    override func loadView() {
        self.view = SKView()
    }

    override func viewDidLoad() {
        super.viewDidLoad()

        if let SKView = self.view as! SKView? {
            let scene = GameScene(size: view.bounds.size)

            SKView.ignoresSiblingOrder = true
            SKView.showsFPS = true
            SKView.showsNodeCount = true
            scene.scaleMode = .resizeFill
            SKView.presentScene(scene)
        }

        self.navigationController?.navigationBar.isHidden = true
    }

    override func viewDidAppear(_ animated: Bool) {
        navigationController?.navigationBar.barStyle = .black
    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

    override var prefersHomeIndicatorAutoHidden: Bool {
        return true
    }
}

我的游戏场景课程

import UIKit
import SpriteKit

class GameScene: SKScene {

    var snakeBlock: SKSpriteNode!

    override func didMove(to view: SKView) {
        layoutScene()
    }

    func layoutScene() {
        backgroundColor = #colorLiteral(red: 0.262745098, green: 0.262745098, blue: 0.262745098, alpha: 1)

        snakeBlock = SKSpriteNode(imageNamed: "GreenBlock")
        snakeBlock.size = CGSize(width: frame.size.width/8.0, height: frame.size.width/8.0)
        snakeBlock.position = CGPoint(x: frame.midX, y: frame.midY)
        addChild(snakeBlock)
    }
}

我希望屏幕上会出现一些东西,尽管它表示有两个节点,但是除了作为第一个节点的场景之外,我什么也看不到。

2 个答案:

答案 0 :(得分:0)

import UIKit
import SpriteKit

class GameViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()

        if let view = self.view as! SKView? {
            // Load the SKScene from 'GameScene.sks'
            if let scene = SKScene(fileNamed: "GameScene") {
                // Set the scale mode to scale to fit the window
                scene.scaleMode = .aspectFill

                // Present the scene
                view.presentScene(scene)
            }

            view.ignoresSiblingOrder = true

            view.showsFPS = true
            view.showsNodeCount = true
        }

        self.navigationController?.navigationBar.isHidden = true
    }

    override var shouldAutorotate: Bool {
        return true
    }

    override var supportedInterfaceOrientations: UIInterfaceOrientationMask {
        if UIDevice.current.userInterfaceIdiom == .phone {
            return .allButUpsideDown
        } else {
            return .all
        }
    }

    override var prefersStatusBarHidden: Bool {
        return true
    }


    override func loadView() {
        self.view = SKView()
    }


    override func viewDidAppear(_ animated: Bool) {
        navigationController?.navigationBar.barStyle = .black
    }

    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }

    override var prefersHomeIndicatorAutoHidden: Bool {
        return true
    }
}

只需更改Apple推荐的viewDidLoad(),当我尝试使用它时,它便对我有效。让我知道这是否适合您

答案 1 :(得分:0)

我没有将这个项目作为游戏项目开始,因此没有GameScene.sks文件,并且场景锚点未设置为视图的中心。为此,我必须在GameScene类的didMove函数中进行设置-

override func didMove(to view: SKView) {
        layoutScene()
        scene?.anchorPoint = CGPoint(x: 0.5, y: 0.5)
    }