我正在为学校目的制作一个 RPG 游戏,我想知道如何在修改后取回整数的原始值。
在我的代码中,我有一个可以进行致命一击的战士,这将使他的攻击在一回合内翻倍,问题是他在游戏的剩余时间里保持他的攻击量......有人有想法吗?
func warriorAbility(_ attacker: Character, _ target: Character){
attacker.weapon.damages *= 2
print("\(attacker.name) the \(attacker.classe) use 'Battlecry !' and do a critical strike of \(attacker.weapon.damages) damages point. He double his atk !")
}
class Sword: Weapon{
init(){
super.init(damages: 10, name: "Sword")
}
} class Fighter : Character{
init(name: String, classe: Classe){
super.init(name: name, healthpoint: 100, maxHealthpoint: 100, weapon: Sword(), classe: classe)
}
编辑:添加了我的战士类和剑类
最佳答案
与其改变武器的属性然后必须恢复它,我建议您在游戏中构建暴击伤害。
类似于:
struct Weapon {
let damage: Int
let criticalMultiplier: Int
let name: String
}
let sword = Weapon(damage: 10, criticalMultiplier:2, name:"sword")
struct Move {
let attacker: Character
let target: Character
let isCritical: Bool
func damage() -> Int {
return attacker.weapon.damage * isCritical ? attacker.weapon.criticalMultiplier : 1
}
}
关于ios - 如何让一个int修改后恢复到原来的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53480231/