我经常收到错误 'Instance member isPaused cannot be used on type GameScene'
I constantly get the error 'Instance member isPaused cannot be used on type GameScene'
这是我的代码,我有一个按钮,当我点击它时,我想暂停游戏以显示菜单...我在出现错误的地方有评论,希望你能看到我做错了什么。
另外我不确定这段代码是否应该在我的 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()
}
}
isPaused
是实例方法,不是 class 方法。您需要在 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()
}
这是我的代码,我有一个按钮,当我点击它时,我想暂停游戏以显示菜单...我在出现错误的地方有评论,希望你能看到我做错了什么。
另外我不确定这段代码是否应该在我的 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()
}
}
isPaused
是实例方法,不是 class 方法。您需要在 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()
}