2016-08-29 69 views
1

標題是一個問題,與此相關的另一個問題是如何將我創建的類連接到UILabel? 任何人都可以幫助我。如果你想知道在SpriteKit中製作文本冒險遊戲的做法。我不知道我是否應該在spritekit或單個視圖應用程序中製作它。爲什麼我得到「實例成員不能用於類型GameViewController」的錯誤?

這裏是我的代碼:

import UIKit 
import SpriteKit 

class GameViewController: UIViewController { 

@IBOutlet weak var locationName: UILabel! // Name of the Location of the game 
@IBOutlet weak var userInputArrows: UILabel! // Arrows for user input 
@IBOutlet weak var objectiveName: UILabel! // Name of the objective of the game 
@IBOutlet weak var computerOutput: UILabel! // The AI's output or return text 
@IBOutlet weak var userInputText: UITextField! // Humans input text 

class Thing { 
    var location: String 
    var name: String 
    var computerDescription: String 
    init(location: String, name: String, computerDescription: String) { 
     self.location = location 
     self.name = name 
     self.computerDescription = computerDescription 

     let location = locationName.text 
    } 
} 
// Main Location is Abandoned Power Plant 
// Start Location: Inside a dim lit room 

let roomAbandonedPowerPlant = Thing(location: "Room", name: "ROOM", computerDescription: "A small dimly lit room " + 
"with large heavy machinery. With almost complete silence.") 

let machineryAbandonedPowerPlant = Thing(location: "Room", name: "MACHINERY", computerDescription: "Old corroded " + 
"machines, looks like it hasn't been used in a long time.") 

let paperAbandonedPowerPlant = Thing(location: "Room", name: "PAPER", computerDescription: "All crumbled up with " + 
"dirt covering everything, and the lettering fading away.") 

let writingAbandonedPowerPlant = Thing(location: "Room", name: "Paper WRITING", computerDescription: "The paper says: " + 
"The only way out of this room is to use your imagination.") 

let machineryLetteringPowerPlant = Thing(location: "Room", name: "MACHINE LETTERING", computerDescription: "It says:" + 
"Built - 1890, Occupation - Used for spare oil, Made By - John Mac") 

let firstKeychainPowerPlant = Thing(location: "Room", name: "KEYCHAIN", computerDescription: "Slightly shining in the dim" + 
" sunlight.") 

let keychainPowerPlant = Thing(location: "Room", name: "VIEW KEYCHAIN", computerDescription: "It say's: Owner: John Mac") 


class Room: Thing { 
    } 
let room = Room(location: "Room", name: "Room", computerDescription: "A small dimly lit room with large heavy " + 
"machinery. With almost complete silence.") 

class Machinery: Thing { 
    } 
let machinery = Machinery(location: "Room", name: "Machinery", computerDescription: "Old corroded machines, looks " + 
"like it hasn't been used in a long time.") 

class Paper: Thing { 
    } 
let paper = Paper(location: "Room", name: "Paper", computerDescription: "All crumbled up with dirt covering " + 
"everything, and the lettering fading away.") 

class PaperWriting: Thing { 
    } 
let paperwriting = PaperWriting(location: "Room", name: "Paper Writing", computerDescription: "The Paper says: " + 
"The only way out of this room is to use your imagination.") 

class MachineLettering: Thing { 
    } 
let machinelettering = MachineLettering(location: "Room", name: "Machine Lettering", computerDescription: "It says: " + 
"Built - 1890, Occupation - Used fir spare oil, Made By - John Mac") 

class Keychain: Thing { 
    } 
let keychain = Keychain(location: "Room", name: "Keychain", computerDescription: "Slightly shining in the dim sunlight.") 

class ViewKeychain: Thing { 
    } 
let viewkeychain = ViewKeychain(location: "Room", name: "View Keychain", computerDescription: "It says: Owner: John " + 
"Mac") 

override func viewDidLoad() { 
    super.viewDidLoad() 

} 

}

+0

你的代碼的哪一行返回錯誤,你在問題的標題中提到? – pedrouan

+0

這是 - 讓位置= locationName.text –

回答

1

您收到的錯誤,因爲在每迅速類接收它自己的名字空間,locationNameThing定義。你的大問題是encapsulation之一。雖然有可能擁有這樣的嵌套類,但它並不常見。相反,類或對象應該是具有其自己的屬性的離散實體以及對這些屬性進行操作的方法。

我會移動Thing及其子類GameViewController之外。 (或者甚至更好,將它移到它自己的文件中)。例如:

class Thing { 
    var location: String 
    var name: String 
    var computerDescription: String 

    init(location: String, name: String, computerDescription: String) 
    { 
     self.location = location 
     self.name = name 
     self.computerDescription = computerDescription 
    } 
} 

class Machinery: Thing { 
} 

class Paper: Thing { 
} 

class PaperWriting: Thing { 
} 

class MachineLettering: Thing { 
} 

class Keychain: Thing { 
} 

class ViewKeychain: Thing { 
} 

class Room: Thing {} 

class GameViewController: UIViewController 
{ 

    @IBOutlet weak var locationName: UILabel! // Name of the Location of the game 
    @IBOutlet weak var userInputArrows: UILabel! // Arrows for user input 
    @IBOutlet weak var objectiveName: UILabel! // Name of the objective of the game 
    @IBOutlet weak var computerOutput: UILabel! // The AI's output or return text 
    @IBOutlet weak var userInputText: UITextField! // Humans input text 

    // Main Location is Abandoned Power Plant 
    // Start Location: Inside a dim lit room 

    let roomAbandonedPowerPlant = Thing(location: "Room", name: "ROOM", computerDescription: "A small dimly lit room " + 
     "with large heavy machinery. With almost complete silence.") 

    let machineryAbandonedPowerPlant = Thing(location: "Room", name: "MACHINERY", computerDescription: "Old corroded " + 
     "machines, looks like it hasn't been used in a long time.") 

    let paperAbandonedPowerPlant = Thing(location: "Room", name: "PAPER", computerDescription: "All crumbled up with " + 
     "dirt covering everything, and the lettering fading away.") 

    let writingAbandonedPowerPlant = Thing(location: "Room", name: "Paper WRITING", computerDescription: "The paper says: " + 
     "The only way out of this room is to use your imagination.") 

    let machineryLetteringPowerPlant = Thing(location: "Room", name: "MACHINE LETTERING", computerDescription: "It says:" + 
     "Built - 1890, Occupation - Used for spare oil, Made By - John Mac") 

    let firstKeychainPowerPlant = Thing(location: "Room", name: "KEYCHAIN", computerDescription: "Slightly shining in the dim" + 
     " sunlight.") 

    let keychainPowerPlant = Thing(location: "Room", name: "VIEW KEYCHAIN", computerDescription: "It say's: Owner: John Mac") 

    let room = Room(location: "Room", name: "Room", computerDescription: "A small dimly lit room with large heavy " + 
     "machinery. With almost complete silence.") 

    let machinery = Machinery(location: "Room", name: "Machinery", computerDescription: "Old corroded machines, looks " + 
     "like it hasn't been used in a long time.") 

    let paper = Paper(location: "Room", name: "Paper", computerDescription: "All crumbled up with dirt covering " + 
     "everything, and the lettering fading away.") 


    let paperwriting = PaperWriting(location: "Room", name: "Paper Writing", computerDescription: "The Paper says: " + 
     "The only way out of this room is to use your imagination.") 

    let machinelettering = MachineLettering(location: "Room", name: "Machine Lettering", computerDescription: "It says: " + 
     "Built - 1890, Occupation - Used fir spare oil, Made By - John Mac") 

    let keychain = Keychain(location: "Room", name: "Keychain", computerDescription: "Slightly shining in the dim sunlight.") 

    let viewkeychain = ViewKeychain(location: "Room", name: "View Keychain", computerDescription: "It says: Owner: John " + 
     "Mac") 

    var currentThing: Thing? 
    { 
     didSet 
     { 
      self.locationName.text = self.currentThing?.location 
     } 
    } 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     self.currentThing = self.paper 

    } 

} 

而且,目前還不清楚,如果Thing的子類都只是存根以後將得到更多的實現,但如果他們的Thing簡單的重命名你應該使用typealiastypealias Room = Thing而非class Room: Thing {}

我很困惑你的意思是把班級連接到UILabel。但是你可以實現一個屬性的觀察者,將每一個屬性設置,例如時間更新標籤:

var currentThing: Thing? 
{ 
    didSet 
    { 
     self.locationName.text = self.currentThing?.location 
    } 
} 

override func viewDidLoad() { 
    super.viewDidLoad() 
    self.currentThing = self.paper 

} 

這裏,屬性currentThing被聲明爲可選Thing。每當currentThing設置爲locationName的文本設置爲currentThinglocationcurrentThing設置爲paper因此locationName應爲「Room」。

您目前只使用UIKit,因此SpriteKit應用程序沒有意義。基於單一視圖應用程序模板的應用程序會更有意義。

+0

感謝beyowulf,我會嘗試你在說什麼。我會給你輸出。 –

+0

謝謝beyowulf它的工作。 –

+0

您不能從內部類訪問外部類。你必須首先創建一個外部類的實例,並且不推薦你在做什麼。另外,創建新的Swift文件來保存一個類。不要將它們全部放在同一個文件中。 –

相關問題