我正在尝试通过本教程在Swift的SpriteKit中制作一个Invader游戏:

https://www.raywenderlich.com/1167-how-to-make-a-game-like-space-invaders-with-spritekit-and-swift-part-1

我完成了part1和part2。但是入侵者的举动仍然很奇怪。当入侵者到达屏幕的右边缘时,只有一个入侵者下线并向左移动。其他人只是向左移动。

我该如何解决?

swift - 无法在SpriteKit中正确移动入侵者-LMLPHP

enum InvaderMovementDirection {
        case right
        case left
        case downThenRight
        case downThenLeft
        case none
    }

func moveInvaders(forUpdate currentTime: CFTimeInterval) {

        if (currentTime - timeOfLastMove < timePerMove) {
            return
        }

        enumerateChildNodes(withName: InvaderType.name) { node, stop in

            switch self.invaderMovementDirection {
            case .right:
                node.position = CGPoint(x: node.position.x + 10, y: node.position.y)
            case .left:
                node.position = CGPoint(x: node.position.x - 10, y: node.position.y)
            case .downThenLeft, .downThenRight:
                node.position = CGPoint(x: node.position.x, y: node.position.y - 10)
            case .none:
                break
            }

            self.timeOfLastMove = currentTime
            self.determineInvaderMovementDirection()

        }
    }


func determineInvaderMovementDirection() {

        var proposedMovementDirection: InvaderMovementDirection = invaderMovementDirection

        enumerateChildNodes(withName: InvaderType.name) { node, stop in

            switch self.invaderMovementDirection {
            case .right:
                if (node.frame.maxX >= node.scene!.size.width - 1.0) {

                    proposedMovementDirection = .downThenLeft
                    self.adjustInvaderMovement(to: self.timePerMove * 0.8)
                    stop.pointee = true
                }
            case .left:

                if (node.frame.minX <= 1.0) {
                    proposedMovementDirection = .downThenRight
                    self.adjustInvaderMovement(to: self.timePerMove * 0.8)
                    stop.pointee = true
                }

            case .downThenLeft:
                proposedMovementDirection = .left
                stop.pointee = true

            case .downThenRight:
                proposedMovementDirection = .right
                stop.pointee = true

            default:
                break
            }

        }

        if (proposedMovementDirection != invaderMovementDirection) {
            invaderMovementDirection = proposedMovementDirection
        }
    }

最佳答案

我认为您需要移动以下几行:

        self.timeOfLastMove = currentTime
        self.determineInvaderMovementDirection()


处于以下情况所造成的循环之外:

enumerateChildNodes(withName: InvaderType.name)


func moveInvaders(forUpdatefunc moveInvaders(forUpdate中。

您只需将它们移到'}'之后即可。

我认为正在发生的事情是,您要移动一个入侵者,然后将invaderMovementDirection更改为proposedMovementDirection,而只有在所有入侵者都移动后,才应该更改invaderMovementDirection

因此,他们在移动.right,他们撞到了墙,您在处理第一个入侵者。您移动它,然后调用determineInvaderMovementDirection,它将建议的方向设置为.downThenLeft。在determineInvaderMovementDirection的结尾,您可以将入侵者的方向从.downThenLeft设置为proposedMovementDirection

当您处理下一个入侵者时,其方向被(错误地)设置为.downThenLeft。因此,将其向下移动然后向左移动,请调用determineInvaderMovementDirection,将入侵者的方向设置为.left,该方向用于处理所有其他入侵者。

10-07 23:13