我一直在使用Swift开发基于文本的冒险游戏。但是,我似乎无法更改特定类的默认值。
下面是该课程的代码,允许我选择播放器课程
import UIKit
class ClassSelectionController: UIViewController
{
//Default class values
var character = (0, 0, "", 0)
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view.
}
//class button actions
@IBAction func fighterBtn(_ sender: Any)
{
character = (50, 60, "Steal Sword", 18)
performSegue(withIdentifier: "Character", sender: self)
}
@IBAction func wizerdBtn(_ sender: Any)
{
character = (25, 70, "Staff", 15)
performSegue(withIdentifier: "Character", sender: self)
}
@IBAction func thiefBtn(_ sender: Any)
{
character = (30, 60, "Dagger", 18)
performSegue(withIdentifier: "Character", sender: self)
}
@IBAction func archerBtn(_ sender: Any)
{
character = (50, 60, "Bow & Arrow", 16)
performSegue(withIdentifier: "Character", sender: self)
}
override func prepare(for segue: UIStoryboardSegue, sender: (Any)?)
{
//code for segue
var vc = segue.destination as! ViewController
vc.finalCharacter = self.character
}
}
这是接收播放器类数据并显示它的类。
import UIKit
class ViewController: UIViewController
{
var finalCharacter = (0, 0, "", 0)
//****************************************
//Setup for outlets go between these lines
//****************************************
@IBOutlet weak var healthLabel: UILabel!
@IBOutlet weak var damageLabel: UILabel!
@IBOutlet weak var weaponLabel: UILabel!
@IBOutlet weak var armorLabel: UILabel!
@IBOutlet weak var storyLabel: UILabel!
@IBOutlet weak var actionButton: UIButton!
@IBOutlet weak var northBtn: UIButton!
@IBOutlet weak var southBtn: UIButton!
@IBOutlet weak var eastBtn: UIButton!
@IBOutlet weak var westBtn: UIButton!
//****************************************
//Setup for outlets go between these lines
//****************************************
override func viewDidLoad()
{
super.viewDidLoad()
// Do any additional setup after loading the view.
healthLabel.text = "Health: \(finalCharacter.0)"
damageLabel.text = "Damage: \(finalCharacter.1)"
weaponLabel.text = "Weapon: " + finalCharacter.2
armorLabel.text = "Armor : \(finalCharacter.3)"
}
//****************************************
//Setup for buttons go between these lines
//****************************************
@IBAction func actionButton(_ sender: Any)
{
}
@IBAction func northButton(_ sender: Any)
{
}
@IBAction func southButton(_ sender: Any)
{
}
@IBAction func eastButton(_ sender: Any)
{
}
@IBAction func westButton(_ sender: Any)
{
}
//****************************************
//Setup for buttons go between these lines
//****************************************
}
即使我的代码当前未显示它,我也确实在类按钮中放置了打印值,以便查看选择类时这些值是否已更改,并且我发现按下按钮时它们确实发生了更改。我还查看了ViewController并更改了finalCharacter值,以查看是否影响了任何东西,但没有影响。因此,我有根据的猜测是问题必须出在ClassSelectionController中。
因此,主要问题是我将单击向导播放器类,并且希望弹出该向导的类统计信息(即25、70,“ Staff”,15),但我只会获取ClassSelectionController中的默认值(0、0,“”,0)。
答案 0 :(得分:2)