ios - 尝试更新文本(特别是游戏中老板的健康状况)并获得 "exc_bad_instruction (code=exc_i386_invop subcode=0x0)"

标签 ios swift xcode

相关代码:

导入 SpriteKit

游戏场景类:SKScene {

//1
let warrior = SKSpriteNode(imageNamed: "warr")

var healthBoss = SKLabelNode(text: "Boss HP: 25")

override func didMove(to view: SKView){
    gameScene = self
    //2
    setupLayers()


    spawnWarr(at: warriorposition)
    spawnBoss(at: bossposition)

    setupLabels()

    backgroundColor = SKColor.blue




}


func setupLayers() {
    objectsLayer = SKNode()
    objectsLayer.name = "Objects Layer"
    addChild(objectsLayer)
}


func spawnWarr(at: CGPoint) {

    let newWarr = warriorclass()
    newWarr.position = at
    self.addChild(newWarr)

}

func spawnBoss(at: CGPoint) {

    let newBoss = bossclass()
    newBoss.position = at
    self.addChild(newBoss)

}

func setupLabels() {

    healthBoss.position = CGPoint(x: size.width * 0.5, y: size.height * 0.1)
    healthBoss.fontColor = whiteColor
    healthBoss.fontName = "Copperplate"
    healthBoss.text = "Boss HP: \(spawnedBoss.health)" 

\特别是这里使用 (spawnedBoss.health) 是我得到“exc_bad_instruction (code=exc_i386_invop subcode=0x0)”的地方

    healthBoss.fontSize = 22
    healthBoss.zPosition = layers.uistuff
    objectsLayer.addChild(healthBoss)

}

bossclass.swift 文件-

导入 UIKit 导入 SpriteKit

类老板类:角色,pTargetable {

var health = 25
var maxhealth = 25

override init() {


    super.init()

    scorePoints = 5

    let texture = texturesBoss
    let xSize = texture.size().width*bossscale
    let ySize = texture.size().height*bossscale
    let size = CGSize(width: xSize, height: ySize)

    self.name = "boss"

    let top = SKSpriteNode (texture: texture, size: size)
    top.zPosition = layers.raiders
    top.color = SKColor(red: 1.0, green: 1.0, blue: 1.0, alpha: 1.0)
    top.colorBlendFactor = 1.0

    self.addChild(top)

}


required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func takeDamage(damage: Int) {
    health -= damage
    print("You lost \(damage) hit points")

    if health <= 0 {
        die()
        print("You are dead now")
    }
}

global.swift 文件包括: var spawnedBoss: bossclass!

和协议(protocol):

协议(protocol) pTargetable {

var health: Int { get set }
func takeDamage(damage: Int)

因此,据我所知,问题是没有。我想也许我在生成 boss 之前引用了 boss 的健康状况,但查看代码似乎并非如此。也许有一个更简单的解决方案来让 Boss 健康栏随伤害更新?

如有任何帮助,我们将不胜感激。谢谢。

最佳答案

当你写的时候:

var spawnedBoss: bossclass!

您向 Swift promise ,如果它为 nil,您将永远不会尝试使用 spawnedBoss,并且它不应该强制您检查。

然后你写道:

healthBoss.text = "Boss HP: \(spawnedBoss.health)" 

此时 spawnedBoss 可能为 nil。

所以,我会改变:

var spawnedBoss: bossclass!

var spawnedBoss: bossclass?

然后添加对 nil 的所有正确检查,以清除您将得到的编译器错误。当您全部清除它们时(不是使用 !,而是使用 if let 或 ??),那么您可以确定仅当 spawnedBoss 不是 nil 时才使用它。

关于ios - 尝试更新文本(特别是游戏中老板的健康状况)并获得 "exc_bad_instruction (code=exc_i386_invop subcode=0x0)",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46434056/

相关文章:

ios - 使用 AVPlayer 实现流畅的视频擦洗

ios - 附加文本时,由 NSTextStorage 支持的 UITextView 未更新

ios - UIView hitTest 阻止点击 subview

ios - 我如何解决因内存错误而终止

ios - viewWillAppear 期间的静态 UITableViewCell 更改未反射(reflect)在显示中

ios - UICollectionViewController 中的 collectionView 可选

iphone - 检测每秒音频文件的幅度?

ios - iOS 中使用 Swift 的 Restful 应用程序

xcode - Jenkins vs.Xcode插件-CodeSign麻烦

ios - UITextView ScrollView 直接显示第二行