如何为skView创建插座?

时间:2015-11-10 20:49:42

标签: ios xcode swift button view

我遇到与此问题相同的问题:SpriteKit GameScene function won't add SKSpriteNode after being called by button

我没有理解答案。如何为skView创建插座?

非常感谢你给我带来的任何澄清!

这是我的代码:

在我的GameScene.swift

   override func didMoveToView(view: SKView) {
        loadMyView()
   }

   func loadMyView(){
        currentScore = 0
        loadSpecialView()
   }

   func loadSpecialView(){
        Person = SKSpriteNode(imageNamed: "Person")
        Person.size = CGSize(width:40, height: 7)
        Person.position = CGPoint(x: self.frame.width / 2, y: self.frame.height / 2 + 120)
        Person.zRotation = 3.14/2
        Person.zPosition = 2.0


        self.addChild(Person)
   }

当玩家死亡时,我的GameviewController.swift中会出现一个带有以下代码的重启按钮:

var currentGame: GameScene!

override func viewDidLoad() {
        super.viewDidLoad()
        currentGame = GameScene()

        if let scene = GameScene(fileNamed:"GameScene") {
            // Configure the view.
            let skView = self.view as! SKView
            skView.showsFPS = true
            skView.showsNodeCount = true

            /* Sprite Kit applies additional optimizations to improve rendering performance */
            skView.ignoresSiblingOrder = true

            /* Set the scale mode to scale to fit the window */
            scene.scaleMode = .ResizeFill

            scene.viewController = self
            skView.presentScene(scene)

        }
}

@IBAction func restartGame(sender: UIButton) {
        outBuy.hidden = true
        outRestart.hidden = true
        currentGame.loadMyView()

    }

当我启动模拟器时,一切正常并且添加了Person。当我按重新启动时,该人不会出现。发生了什么事?

1 个答案:

答案 0 :(得分:0)

我找到了解决方案!最后!

我将currentGame = GameScene()更改为currentGame = scene并将其移到skView.presentScene(scene)上方