- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
标题是一个问题,与该问题相关的另一个问题是如何将我创建的类连接到 UILabel?谁能帮帮我吗。如果您想知道我在做什么,那就是在 SpriteKit 中制作文本冒险游戏。我不知道我是否应该在 spritekit 或单 View 应用程序中制作它。
这是我的代码:
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()
}
最佳答案
您收到该错误是因为在 swift 中每个类都有自己的 namespace 并且 locationName
未在 Thing
中定义。您更大的问题是 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
的子类是否只是 stub ,以后会得到更多的实现,但如果它们只是简单地重命名 Thing
,你应该使用 typealias
typealias 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
的文本都会设置 currentThing
的 location
。 currentThing
设置为 paper
,因此 locationName
应显示为“Room”。
到目前为止,您只使用了 UIKit,因此 SpriteKit 应用程序没有意义。基于单一 View 应用程序模板的应用程序会更有意义。
关于ios - 为什么我会收到 "Instance Member cannot be used on type GameViewController"错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39212306/
我创建了分享按钮,用于在 Facebook 、推特等平台上分享我的分数。在 GameViewController 中创建的按钮以及我在游戏场景中创建的分数和高分。 我确实分享了分数,但我不知道如何将我
运行在spriteKit下的App。几天前,一旦 gameViewController 被加载,gameScene 就会被加载。今天我把它的加载 Action 改成只有在点击开始按钮时才加载gameS
想在游戏开始前预加载 textureAtlases,所以我决定将场景启动代码放入完成处理程序中,但由于某些原因应用程序崩溃了。这是我的代码: import UIKit import SpriteKit
我已将横幅 View 集成到应用程序内的场景中,但无法将插页式广告集成到另一个场景中。这是我的代码:导入 SpriteKit导入游戏包导入 GoogleMobileAds class GameOve
我创建了一个 HomeScene.sks 和一个 HomeScene.swift。在 GameViewController 中,我已将默认的“GameScene”更改为“HomeScene”,如下所示
一旦我的游戏完成,我将在我的 GameViewController 中运行 self.performSegueWithIdentifier("goToGameOver", sender: nil),它
我有两个场景,GameScene 和 PlayScene。我可以展示我的 GameScene,但我无法展示 PlayScene,我使用的是相同的代码和正确的文件名。我正在使用 UIButton 来调用
我正在使用 SpriteKit 框架在 Swift 中开发一款游戏。在此应用程序中,我有 GameScene.sks,其中显示了我的 UI。在该 .sks 文件中,底部菜单有一个 SKReferenc
下面复制相关代码,我也在Github上放了一个简单的测试项目来演示这种情况: https://github.com/prinomen/viewPresentSceneTest 我有一个 GameVie
我成功测试了应用内购买后广告的移除。我只需要在他们完成付款并完成交易后,当前 GameScene 上的横幅广告仍然存在。为了删除它们,我必须重新启动该应用程序。我的 GameViewControlle
所以我在我的 swift 项目中创建了几个不同的 SKScenes 并且我在几个教程中看到你不必将 gameScene.swift 作为你的第一个场景启动,但是我找不到一个教程解释如何 import
我一直在使用 SpriteKit 和 Swift 开发游戏,但我似乎无法确定 GameViewController 和我的任何一个 SKScene 之间的真正区别秒。我试图理解这些差异,因为我想在我的
当我尝试在 Swift 中为我的游戏添加不同的场景时,我遇到了 unarchiveFromFile 方法。这种方法的问题在于它只适用于 GameScene 类。如果你从 extension SKNod
我在 GameViewController 类中创建了一个 UIScrollView,其中 subview 是场景。当用户进入特定场景并按下特定按钮时,ScrollView 必须停止滚动。 我怎样才能
我在 GameViewController 中制作了游戏菜单。然后,当按下 Go 按钮时,我尝试移动到 GameScene。我使用默认代码来呈现 GameScene 文件。 if let scene
我想在 SceneKit 应用程序中使用 Core Data,但是在创建项目时,选择"file"、“新建”-> 后没有选项可以选中“使用核心数据” 项目->游戏;如果我在创建项目后尝试将 Core D
我已将 admob 奖励视频添加到我的应用中。 admob 一切正常。但是,我无法在观看奖励视频后更新直播标签。如果我去另一个场景然后回来,它会更新并显示新值。 //GameViewControlle
在 GameViewController 中,我想从我的一个 SKScenes 访问方法。它的类称为 PlayScene。 我一直在努力解决这个问题,但还没有成功。我正在尝试像 let gameSce
这是我的相关代码: 来 self 的 GameScene 类: func resetCircleAndScore(scale: CGFloat) { circleIsStationar
我在 Storyboard中创建了一个按钮,它位于我的游戏屏幕上。它在 GameViewController() 中有一个 IB Action ,如下所示: @IBAction func but
我是一名优秀的程序员,十分优秀!