我经常收到错误'实例成员isPaused不能用于类型GameScene'

时间:2017-11-23 23:04:46

标签: ios swift sprite-kit gameplay-kit

这是我的代码,我有一个按钮,当我点击它时,我想暂停游戏以显示菜单...我有错误显示的评论,所以你可以希望看到我做错了。

此外,我不确定这段代码是否应该在我的GameScene文件中,如果没有,你能告诉我它应该在哪个。

感谢你们的帮助。

//
//  GameScene.swift
//  Pong2
//
//  Created by Hussain on 10/11/16.
//  Copyright © 2016 Archetapp. All rights reserved.
//

import SpriteKit
import GameplayKit

class GameScene: SKScene {

    var ball = SKSpriteNode()
    var enemy = SKSpriteNode()
    var main = SKSpriteNode()

    var topLbl = SKLabelNode()
    var btmLbl = SKLabelNode()

    var score = [Int]()

    @IBOutlet weak var pauseBetton: UIButton! // HERE IS MY OUTLET


    override func didMove(to view: SKView) {

        func pauseButton (sender: UIButton){ // HERE'S MY FUNCTION TO TRY AND PAUSE MY GAME
            let pauseAction = SKAction.run {
                GameScene.isPaused = true // HERE'S THE LINE I GET MY ERROR
                debugPrint("Paused")
            }
        }



        topLbl = self.childNode(withName: "topLabel") as! SKLabelNode
        btmLbl = self.childNode(withName: "btmLabel") as! SKLabelNode
        ball = self.childNode(withName: "ball") as! SKSpriteNode

        print(self.view?.bounds.height)

        enemy = self.childNode(withName: "enemy") as! SKSpriteNode
        enemy.position.y = (self.frame.height / 2) - 50

        main = self.childNode(withName: "main") as! SKSpriteNode
        main.position.y = (-self.frame.height / 2) + 50

        let border  = SKPhysicsBody(edgeLoopFrom: self.frame)

        border.friction = 0
        border.restitution = 1

        self.physicsBody = border

        startGame()
    }
}

1 个答案:

答案 0 :(得分:0)

isPaused是一个实例方法,而不是类方法。您需要在SKNode的特定实例上调用它,例如您的GameScene实例。

GameScene.isPaused = true更改为self.isPaused = true或仅isPaused = true

除了这个问题,您需要从pauseButton方法中移动didMove(to:)方法。

func pauseButton (sender: UIButton) {
    let pauseAction = SKAction.run {
        self.isPaused = true
        debugPrint("Paused")
    }
}

override func didMove(to view: SKView) {
    topLbl = self.childNode(withName: "topLabel") as! SKLabelNode
    btmLbl = self.childNode(withName: "btmLabel") as! SKLabelNode
    ball = self.childNode(withName: "ball") as! SKSpriteNode

    print(self.view?.bounds.height)

    enemy = self.childNode(withName: "enemy") as! SKSpriteNode
    enemy.position.y = (self.frame.height / 2) - 50

    main = self.childNode(withName: "main") as! SKSpriteNode
    main.position.y = (-self.frame.height / 2) + 50

    let border  = SKPhysicsBody(edgeLoopFrom: self.frame)

    border.friction = 0
    border.restitution = 1

    self.physicsBody = border

    startGame()
}