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

时间:2019-04-24 07:53:19

标签: swift sprite-kit

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

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

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

我该如何解决?

bug

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
        }
    }

1 个答案:

答案 0 :(得分:0)

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

        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,该方向用于处理所有其他入侵者。