gpt4 book ai didi

swift - SKActions 无法识别某些值的变化

转载 作者:可可西里 更新时间:2023-11-01 02:05:52 25 4
gpt4 key购买 nike

我有 3 组 Sprite (EnemyGroups),这些组按特定顺序排列(每组延迟一秒)。请看代码:

敌人集团:

struct EnemyGroup    {
var enemies : [Enemy]
var isVisible : Bool
var isStatic : Bool

func setup () {
if isStatic {
for enemy in enemies {
enemy.sprite.alpha = 1.0
enemy.sprite.isUserInteractionEnabled = false

}
isVisible = true
}
}


mutating func switchEnemiesON () {
if !isStatic {
for enemy in enemies {

enemy.sprite.alpha = 1.0
enemy.sprite.physicsBody?.categoryBitMask = enemyCategory
enemy.sprite.physicsBody?.collisionBitMask = bombCategory

}
isVisible = true
}
}

mutating func switchEnemiesOFF () {
if !isStatic {
for enemy in enemies {
enemy.sprite.alpha = 0.0
enemy.sprite.physicsBody?.collisionBitMask = 0
enemy.sprite.physicsBody?.categoryBitMask = 0
}

isVisible = false
}
}

在游戏场景中:

   func groupActionSequence (enemyGroup: EnemyGroup)  {
for enemy in enemyGroup.enemies {
var enemyGroupWithAction = enemyGroup
let groupSequence = SKAction.sequence([
SKAction.run({ enemyGroupWithAction.switchEnemiesON() }),
SKAction.wait(forDuration: showTime),
SKAction.run({ enemyGroupWithAction.switchEnemiesOFF() }),
SKAction.wait(forDuration: hideTime)
])

let repeatGroupAction = SKAction.repeatForever(groupSequence)
enemy.sprite.run(repeatGroupAction)
}
}

func groupActionSequenceWithDelay (delayTime: TimeInterval, enemyGroup: EnemyGroup, groupKey: String) {
for enemy in enemyGroup.enemies {

var group = enemyGroup
let groupSequence = SKAction.sequence([
SKAction.run({ group.setup() }),
SKAction.wait(forDuration: delayTime),
SKAction.run({ self.groupActionSequence(enemyGroup: enemyGroup) })
])

enemy.sprite.run(groupSequence, withKey: groupKey)
}
}

为敌人群体设置行动:

 groupActionSequenceWithDelay(delayTime: 0, enemyGroup: enemyGroup1, groupKey: "group1")
groupActionSequenceWithDelay(delayTime: 1, enemyGroup: enemyGroup2, groupKey: "group2")
groupActionSequenceWithDelay(delayTime: 2, enemyGroup: enemyGroup3, groupKey: "group3")

我将 enemyGroup1enemyGroup2 设置为 isStatic = false 并将 enemyGroup3 设置为 isStatic = true 。我将 enemyGroup1enemyGroup2 isStatic 更改为 true 操作仍在 enemyGroup1 上运行> 和 enemyGroup2 就好像isStatic = false。我怎样才能让switchEnemiesOFF()switchEnemiesON()识别isStatic`值的变化?另外,我不想停止这些 Action ,因为我想保持每个组的相同顺序,它们的运行 Action 有一秒的差异,这就是为什么我用 if 语句来保护一些函数。

最佳答案

使 EnemyGroup 成为类而不是结构。

因为它是一个结构体,在运行 block 闭包中捕获的 enemyGroupWithAction 变量是您在参数中收到的变量的副本(也是作为参数传递给函数的变量的副本)。这些副本中的每一个都有自己独立的值(Struct 是一种值类型)并且单独存在。如果 enemyGroup 是一个对象(类),则只会捕获引用(类是引用类型)并且对 is 属性的任何修改都会影响原始对象实例。

关于swift - SKActions 无法识别某些值的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42938881/

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