gpt4 book ai didi

swift - SpriteKit SKSprite节点

转载 作者:行者123 更新时间:2023-11-28 15:27:29 26 4
gpt4 key购买 nike

func repulseFire() {
if let zombieGreen = self.childNode(withName: "zombie") as? SKSpriteNode {
self.enumerateChildNodes(withName: "repulse") {
node, stop in
if let repulse = node as? SKSpriteNode {
if let action = zombieGreen.action(forKey: "zombieAction") {
action.speed = 0
func run() {
action.speed = 1
}
var dx = CGFloat(zombieGreen.position.x - repulse.position.x)
var dy = CGFloat(zombieGreen.position.y - repulse.position.y)
let magnitude = sqrt(dx * dx + dy * dy)
dx /= magnitude
dy /= magnitude
let vector = CGVector(dx: 25.0 * dx, dy: 25.0 * dy)
func applyImpulse() {
zombieGreen.physicsBody?.applyImpulse(vector)
}
zombieGreen.run(SKAction.sequence([SKAction.run(applyImpulse), SKAction.wait(forDuration: 0.2), SKAction.run(run)]))
}

}
}
}
}

调用此函数时,我正试图击退僵尸。唯一的问题是在某些时间点现场有不止一只僵尸,并且脉冲仅适用于屏幕上先于其他僵尸生成的僵尸。我怎样才能让所有的僵尸都受到影响?我认为这与“if let zombieGreen = self.childNode(withName: "zombie") as?SKSpriteNode”这一行有关

最佳答案

将僵尸添加到场景时,应考虑使用数组来存储它们。这比枚举场景更快,并为您提供更大的灵 active 。

// create an array of spriteNodes
var zombieArray:[SKSpriteNode]

//add zombies to array when you add them to scene
zombieArray.append(zombieGreen)

//check if any zombies are in the scene
if zombieArray.count > 0{
.....
}

//Do something with all the zombies in the array - your main question.
for zombie in zombieArray{

.....
zombie.physicsBody?.applyImpulse(vector)
}

// remove zombie from array
zombieArray.remove(at: zombieArray.index(of: theZombieYouWantToRemove))

关于swift - SpriteKit SKSprite节点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45045758/

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