【问题标题】:Can't move invaders correctly in SpriteKit无法在 SpriteKit 中正确移动入侵者
【发布时间】:2019-04-24 07:53:19
【问题描述】:

我正在尝试使用本教程在 Swift 中使用 SpriteKit 制作入侵者游戏:

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

我完成了第 1 部分和第 2 部分。但是入侵者的移动还是很奇怪。当入侵者到达屏幕的右边缘时,只有一名入侵者下线并向左移动。其他人只是向左移动。

我该如何解决?

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

【问题讨论】:

    标签: swift sprite-kit


    【解决方案1】:

    我认为你需要移动这些行:

            self.timeOfLastMove = currentTime
            self.determineInvaderMovementDirection()
    

    在以下创建的循环之外:

    enumerateChildNodes(withName: InvaderType.name)
    

    func moveInvaders(forUpdatefunc moveInvaders(forUpdate.

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

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

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

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

    【讨论】:

    • 感谢您的描述!我明白问题出在哪里,而且效果很好!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-03
    • 1970-01-01
    • 2015-06-10
    • 1970-01-01
    相关资源
    最近更新 更多