首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpriteKit SKSpriteNode

SpriteKit SKSpriteNode
EN

Stack Overflow用户
提问于 2017-07-11 22:58:58
回答 2查看 128关注 0票数 0
代码语言:javascript
复制
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”这句话有关。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-07-12 07:57:14

当您将僵尸添加到场景中时,您应该考虑使用Array来存储僵尸。这比列举场景更快,并给了您更多的灵活性。

代码语言:javascript
复制
// 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))
票数 1
EN

Stack Overflow用户

发布于 2017-07-12 09:47:08

场景中所有受所有拒绝节点影响的僵尸

代码语言:javascript
复制
func repulseFire() {
    self.enumerateChildNodes(withName: "zombie") {
            zombieGreen, stop in {

            self.enumerateChildNodes(withName: "repulse") {
                node, stop in
                //etc

场景中的所有僵尸都受到一个拒绝节点的影响

代码语言:javascript
复制
func repulseFire() {
    self.enumerateChildNodes(withName: "zombie") {
            node, stop in {
        if let repulseNode =  self.childNode(withName: "repulse") {
            //etc
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45045758

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档