gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 08:01:52 26 4
gpt4 key购买 nike

相关代码:

导入 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com