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)]))
}
}
}
}
}当这个函数被调用时,我试图回击僵尸。唯一的问题是,在某些时间点,现场有不止一个僵尸,而这种冲动只适用于屏幕上其他人之前产生的僵尸。我怎么做才能让所有的僵尸都受到影响?我认为这与“如果让zombieGreen = self.childNode(withName:”僵尸“) as? SKSpriteNode”这句话有关。
发布于 2017-07-12 07:57:14
当您将僵尸添加到场景中时,您应该考虑使用Array来存储僵尸。这比列举场景更快,并给了您更多的灵活性。
// 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))发布于 2017-07-12 09:47:08
场景中所有受所有拒绝节点影响的僵尸
func repulseFire() {
self.enumerateChildNodes(withName: "zombie") {
zombieGreen, stop in {
self.enumerateChildNodes(withName: "repulse") {
node, stop in
//etc场景中的所有僵尸都受到一个拒绝节点的影响
func repulseFire() {
self.enumerateChildNodes(withName: "zombie") {
node, stop in {
if let repulseNode = self.childNode(withName: "repulse") {
//etchttps://stackoverflow.com/questions/45045758
复制相似问题