gpt4 book ai didi

ios - 将之前计算的数据添加到每次按下 UIButton 时生成的新数据中

转载 作者:行者123 更新时间:2023-11-29 05:15:35 24 4
gpt4 key购买 nike

我正在处理 3 个主要数据: 伤害经验, 起始攻击等级, 当前攻击级别。

当按下 UIButton Attack 时,会获得 DamageExp,然后将其添加到startingAttackLevel 中,从而得到 currentAttackLevel。

现在我想做的是每当再次按下按钮时添加获得的最新伤害Exp并将其添加到之前的currentAttackLevel,以创建新的CurrentLevel等等。

我创建了一个名为 Enemy 的类和一个名为 attack() 的方法。但我只是不知道如何保存当前数据,然后能够再次点击 UIButton 并将最新的 xp 添加到之前的 currentAttackLevel。

这可能很简单,但我不知道我需要知道什么。

这是我的类(class)敌人.swift

class Enemy  {
var healthLevel = 100

func health() {
print("You currently have \(healthLevel) health")
}

func move(steps: Int) -> String {
return "Walk forward \(steps) paces"
}

func attack() {
let startingAttackLevel: Float = 10.0
var currentAttackLevel: Float = 10.0
var attackStrength = currentAttackLevel
var attackRoll = Float.random(in: 0.0...((currentAttackLevel * 0.20) + currentAttackLevel))
var attackDamage = attackRoll * attackStrength
var damageExp: Float = 0.0

if attackRoll > (currentAttackLevel - (currentAttackLevel * 0.20)) && attackRoll < ((currentAttackLevel * 0.20) + currentAttackLevel) {
damageExp = currentAttackLevel * 0.30
currentAttackLevel = damageExp + startingAttackLevel

} else if attackRoll > (currentAttackLevel - (currentAttackLevel * 0.60)) && attackRoll < (currentAttackLevel - (currentAttackLevel * 0.20)){
damageExp = currentAttackLevel * 0.15
currentAttackLevel = damageExp + startingAttackLevel

} else if attackRoll > 0.0 && attackRoll < (currentAttackLevel - (currentAttackLevel * 0.60)){
damageExp = currentAttackLevel * 0.06
currentAttackLevel = damageExp + startingAttackLevel
}

print("You roll a \(String(format: "%.0f", attackRoll)). You do a total damage of \(String(format: "%.1f", attackDamage)) damage. ")
print("You have gained a total amount of \(damageExp) xp")
print("Your are currently level \(currentAttackLevel)")
}
}

下面是我的 viewController.swift 记住我的 Enemy() 类是 = 我的常量骨架

import UIKit

class ViewController: UIViewController {

let skeleton = Enemy()

@IBOutlet weak var mainTextLabel: UILabel!

@IBAction func walkForward(_ sender: UIButton) {

print(skeleton.move(steps: Int.random(in: 0...50)))

}

@IBAction func attack(_ sender: UIButton) {

skeleton.health()
skeleton.attack()

}
}

最佳答案

您可以使用以下代码 -

class Enemy  {
static let initialHealthValueConstant : Float = 100.0

let initialHealthLevel : Float = Enemy.initialHealthValueConstant
var currentHealthLevel : Float

init(with currentHealthLevel : Float) {
self.currentHealthLevel = currentHealthLevel
}

convenience init() {
self.init(with: Enemy.initialHealthValueConstant)
}

func health() {
print("You currently have \(currentHealthLevel) health")
}

func move(steps: Int) -> String {
return "Walk forward \(steps) paces"
}

func attack() {
let startingAttackLevel = currentHealthLevel
var currentAttackLevel = currentHealthLevel
let attackStrength = currentAttackLevel
let attackRoll = Float.random(in: 0.0...((currentAttackLevel * 0.20) + currentAttackLevel))
let attackDamage = attackRoll * attackStrength
var damageExp: Float = 0.0

if attackRoll > (currentAttackLevel - (currentAttackLevel * 0.20)) && attackRoll < ((currentAttackLevel * 0.20) + currentAttackLevel) {
damageExp = currentAttackLevel * 0.30
currentAttackLevel = damageExp + startingAttackLevel

} else if attackRoll > (currentAttackLevel - (currentAttackLevel * 0.60)) && attackRoll < (currentAttackLevel - (currentAttackLevel * 0.20)){
damageExp = currentAttackLevel * 0.15
currentAttackLevel = damageExp + startingAttackLevel

} else if attackRoll > 0.0 && attackRoll < (currentAttackLevel - (currentAttackLevel * 0.60)){
damageExp = currentAttackLevel * 0.06
currentAttackLevel = damageExp + startingAttackLevel
}

currentHealthLevel = currentAttackLevel
print("You roll a \(String(format: "%.0f", attackRoll)). You do a total damage of \(String(format: "%.1f", attackDamage)) damage. ")
print("You have gained a total amount of \(damageExp) xp")
print("Your are currently level \(currentAttackLevel)")
}
}

此外,您不需要这两个额外的变量 - currentAttackLevel、startingAttackLevel。您可以使用 currentHealthLevel 来代替它们。

关于ios - 将之前计算的数据添加到每次按下 UIButton 时生成的新数据中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59222557/

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