2016-08-29 2 views
1

Der Titel ist eine Frage und die andere Frage zu diesem einen ist, wie verbinde ich die Klasse, die ich erstellt habe mit dem UILabel? Kann mir bitte jemand helfen. Wenn Sie sich fragen, was es macht, ein Text-Abenteuer-Spiel in SpriteKit zu machen. Ich weiß nicht, ob ich es in Spritekit oder einer einzigen Ansichtsanwendung machen sollte.Warum erhalte ich den Fehler "Instanz-Member kann nicht vom Typ GameViewController verwendet werden"?

Hier ist mein Code:

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

Welche Zeile Ihres Codes gibt den Fehler zurück, den Sie im Titel Ihrer Frage angeben? – pedrouan

+0

Es ist - lass location = locationName.text –

Antwort

1

Sie erhalten diesen Fehler, weil in swift jede Klasse einen eigenen Namensraum erhält und locationName nicht in Thing definiert. Ihr größeres Problem ist eines von encapsulation. Obwohl es möglich ist, geschachtelte Klassen wie diese zu haben, ist es ungewöhnlich. Vielmehr sollten Klassen oder Objekte diskrete Entitäten mit eigenen Eigenschaften und Methoden sein, die auf diesen Eigenschaften basieren.

Ich würde Thing und seine Unterklassen außerhalb GameViewController verschieben. (Oder noch besser, verschieben Sie es in eine eigene Datei). Zum Beispiel:

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 

    } 

} 

Außerdem ist es nicht klar, ob Thing ‚s Subklassen nur Stubs sind, die später mehr Implementierung bekommen, aber wenn sie einfache Umbenennung von Thing sind, sollten Sie typealiastypealias Room = Thing statt class Room: Thing {} zu verwenden.

Ich bin verwirrt, was Sie meinen, indem Sie die Klasse an die UILabel anschließen. Aber Sie können eine Eigenschaft Beobachter implementieren, dass das Etikett jedes Mal, wenn eine Eigenschaft aktualisiert wird beispielsweise festgelegt:

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

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

} 

Hier wird eine Eigenschaft currentThing ist als optionales Thing erklärt. Jedes Mal, wenn currentThinglocationName gesetzt ist, wird der Text currentThing 's location gesetzt. currentThing wird auf paper gesetzt, also sollte locationName "Room" lauten.

Sie verwenden UIKit bisher nur so weit, dass eine SpriteKit-Anwendung keinen Sinn ergibt. Eine Anwendung basierend auf der Single-View-Anwendungsvorlage wäre sinnvoller.

+0

Danke beyowulf, ich werde versuchen, was du gesagt hast. Ich gebe dir die Ausgabe. –

+0

Danke beyowulf es hat funktioniert. –

+0

Sie können nicht auf die äußere Klasse von der Inside-Klasse zugreifen. Sie müssten zuerst eine Instanz der äußeren Klasse erstellen, und was Sie tun, wird nicht empfohlen. Erstellen Sie außerdem neue Swift-Dateien, um eine Klasse zu speichern. Behalte sie nicht alle in der gleichen Datei. –

Verwandte Themen