我目前正在学习 Swift,尤其是 Mac,我试图制作一种口袋妖怪对战游戏。为此,我想显示健康点的进度条,但我只能找到条纹的。我不知道我是否可以让它们根据给定值进行更改,或者我是否没有使用正确类型的 IBOutlet。
如果有人知道如何显示进度条,这就是我所需要的 :)
谢谢
(我是法国人,请原谅我可能犯的错误...)
编辑:这是我的一些代码
变量:
class AppDelegate: NSObject, NSApplicationDelegate {
@IBOutlet var window: NSWindow?
var firstPokemon:Pokemon = Pokemon(name: "Aquali", level: 50, HP: 245, power: 10)
//[...]
@IBOutlet var firstPokemonHealthBar: NSProgressIndicator!
//[...]
var firstPokemonOriginalHP:Float = 0
var secondPokemon:Pokemon = Pokemon(name: "Pikachu", level: 90, HP: 300, power: 13)
//[...]
@IBOutlet var secondPokemonHealthBar: NSProgressIndicator!
//[...]
var secondPokemonOriginalHP:Float = 0
初始化:
func applicationDidFinishLaunching(aNotification: NSNotification?)
{
firstPokemonOriginalHP = firstPokemon.HP
secondPokemonOriginalHP = secondPokemon.HP
}
更新(每次攻击后调用)
func update(str:String)
{
if firstPokemon.HP < 0 {firstPokemon.HP = 0}
if secondPokemon.HP < 0 {secondPokemon.HP = 0}
//firstPokemonName.stringValue = firstPokemon.name
//firstPokemonLevel.stringValue = "Level: " + String(Int(firstPokemon.level))
//firstPokemonHP.stringValue = "\(Int(firstPokemon.HP))/\(Int(firstPokemonOriginalHP))hp"
firstPokemonHealthBar.doubleValue = Double(firstPokemon.HP/firstPokemonOriginalHP)
//firstPokemonPicture.image = NSImage(named: "\(firstPokemon.name)")
//firstPokemonPicture.image.size = firstPokemonPicture.bounds.size
//secondPokemonName.stringValue = secondPokemon.name
//secondPokemonLevel.stringValue = "Level: \(Int(secondPokemon.level))"
//secondPokemonHP.stringValue = "\(Int(secondPokemon.HP))/\(Int(secondPokemonOriginalHP))hp"
secondPokemonHealthBar.doubleValue = Double(secondPokemon.HP/secondPokemonOriginalHP)
//secondPokemonPicture.image = NSImage(named: "\(secondPokemon.name)")
//secondPokemonPicture.image.size = secondPokemonPicture.bounds.size
//TextBoxInfos.stringValue = str
if firstPokemon.HP <= 0 || secondPokemon.HP <= 0
{
button1.enabled = false
button2.enabled = false
battleEnded()
}
}
最佳答案
好的 NSLevelIndicator 工作了! 我只需要确保每个条形图的“样式”都是连续的,并且最大值是 1 而不是预定义的 2。谢谢让·巴蒂斯特
(来源:hostingpics.net)
关于macos - 如何在 Mac Cocoa App (Swift) 上显示进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25169917/