How to add individual integer variables for each SKSpriteNode spawned
我在 swift 中创建了一个小行星类型的游戏,其中我有多种类型的敌人。我目前有一个敌人被一击摧毁。我正在尝试创建第二个敌人,该敌人被两次打击摧毁。
我在类级别分配了一个变量"varenemyTwoHealth = 0",然后在我为其创建敌人、物理体、路径等的函数中添加了"self.enemyTwoHealth = 2"。在我的"didBegin(_contact:SKPhysicsContact)"内部,当导弹与敌人相撞时,我减少"enemyTwoHealth -= 1"并检查"ifenemyTwoHealth <= 0 { body2.node?.removeFromParent()}"。
我遇到的问题是,当第二个敌人在杀死第一个敌人之前出现在屏幕上时,它会将他们的所有生命值再次重置为 2。 (例如,EnemyType2 生成,它有 2 生命值,我击中它一次,现在它有 1 生命值;第二个 EnemyType2 生成,现在都有 2 生命值。)
如何将健康变量分配给每个单独的敌人?
(代码很多,所以我试着只包含相关代码)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | class GameScene: SKScene, SKPhysicsContactDelegate { var enemyTwoHealth = 0 func didBegin(_ contact: SKPhysicsContact) { if body1.categoryBitMask == PhysicsCategories.Missle && body2.categoryBitMask == PhysicsCategories.EnemyTwo { if enemyTwoLives <= 0 { body2.node?.removeFromParent() addScore() } else { enemyTwoHealth -= 1 } body1.node?.removeFromParent() } } func spawnEnemyTwo() { self.enemyTwoLives = 2 } } |
您至少有四个选项,其中 2 个选项已被评论建议。
因为这是一个小行星游戏,我根本不会制造"健康"。在你的情况下,我会做的是拥有一个 SKSpriteNodes 的子继承权。
你的层次结构应该是这样的:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 | Biggest asteroid - zPosition = 5 | -- Bigger asteroid - zPosition = 4 | | | -- Big asteroid - zPosition = 3 | | | | | -- Small asteroid - zPosition = 2 | | | | | | | -- Smaller asteroid - zPosition = 1 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | -- Smaller asteroid - zPosition = 1 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | -- Small asteroid - zPosition = 2 | | | | | | | -- Smaller asteroid - zPosition = 1 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | -- Smaller asteroid - zPosition = 1 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | -- Big asteroid - zPosition = 3 | | | | | -- Small asteroid - zPosition = 2 | | | | | | | -- Smaller asteroid - zPosition = 1 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | -- Smaller asteroid - zPosition = 1 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | -- Small asteroid - zPosition = 2 | | | | | | | -- Smaller asteroid - zPosition = 1 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | -- Smaller asteroid - zPosition = 1 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | -- Big asteroid - zPosition = 3 | | | | | -- Small asteroid - zPosition = 2 | | | | | | | -- Smaller asteroid - zPosition = 1 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | -- Smaller asteroid - zPosition = 1 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | -- Small asteroid - zPosition = 2 | | | | | | | -- Smaller asteroid - zPosition = 1 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | -- Smaller asteroid - zPosition = 1 | | | | | | | | | -- Smallest asteroid - zPosition = 0 | | | | | | | | | -- Smallest asteroid - zPosition = 0 |
所有这些小行星都将位于 (0,0) 位置,除了 zPosition 最高的节点。
然后,当你的游戏进行时,当你最大的小行星被击中时,你有一种方法可以将它的直系子代(不是孙子代)从小行星上取下并带到现场。然后,您应用您的代码,使它们根据Bullets的进入方式向不同方向投射。
每次小行星被击中时,您将孩子移动到场景并移除父母。
最终你会找到最小的没有孩子的小行星,从而摧毁所有的小行星。
这种方法在 RAM 上会有点贵,但你不必担心不断地创建新对象会丢失 CPU 周期。
你可以做的另一件事是子类 SKSpriteNode 给每个类。
1 2 3 4 5 6 7 | class Rocks : SKSpriteNode{ var health = 0 convenience init(health:Int){ self.init(imageNamed:"rock") self.health = health } } |
您可以做的另一件事是将运行状况添加到 userData:
1 2 3 4 5 6 7 8 9 10 | let userData = rock.userData ?? [:]() userData["health"] = 2 rock.userData = userData .... if let health = rock.userData["health"]{ health -= 1 rock.userData["health"] = 1 } |
最后,您可以使用 GamePlayKit 并添加一个健康组件:
1 2 3 4 5 6 7 8 9 10 11 12 | class HealthComponent : GKComponent{ var health = 2 } .... var healthComponent = new HealthComponent() rock.entity.addComponent(healthComponent) .... if let healthComponent = rock.entity.component(ofType:HealthComponent){ healthComponent.health -= 1 } |