让 SKSpriteNode 角色使用 SKSpriteNode 平台移动



我正在构建一个平台游戏,它有一个跳到移动平台上的SKSpriteNode角色。

当平台移动时,角色不会移动,最终会从平台上掉下来。如果我点击移动按钮,角色会很好地移动,但我希望角色在移动时"粘"在平台上。

我还没有发布代码,因为代码正在按预期工作。我有一种感觉,这是我可以设置的属性?

编辑:

我有一个解决方案 - 只是不确定人们应该如何做,所以我会在下面发布并等待反馈。它又长又复杂,可能不需要。

当用户按下向左或向右方向按钮时,我会通过以下方式从 GameScene 中移动场景.swift:

    func moveGround(direction: String) {

    if direction == "left" {
        [snip...]
        // move the platforms
        self.enumerateChildNodesWithName("platform") {
            node, stop in
            if let foundNode = node as? PlatformSprite {
                node.position.x += Helper().kMovingDistance
            }
        }
        // move all other nodes
        self.enumerateChildNodesWithName("*") {
            node, stop in
            if let foundNode = node as? SKSpriteNode {
                node.position.x += Helper().kMovingDistance
            }
        }
        [snip...]
    } else if direction == "right" {
        [snip...]
        // move the platforms
        self.enumerateChildNodesWithName("platform") {
            node, stop in
            if let foundNode = node as? PlatformSprite {
                node.position.x -= Helper().kMovingDistance
            }
        }
        // move all other nodes
        self.enumerateChildNodesWithName("*") {
            node, stop in
            if let foundNode = node as? SKSpriteNode {
                node.position.x -= Helper().kMovingDistance
            }
        }
        [snip...]
    }

这很好地推动了场景的发展。然后,当角色降落在平台顶部时,我使用 SKAction 序列启动平台移动,并通过将反向序列发送到 GameScene 来启动场景移动:

    func startMoving() {
    if !self.isMoving {
        [snip...]
        // get the platform actions and initiate the movements
        let actions = self.getMovements()
        let seq:SKAction = SKAction.sequence(actions)
        // move the platform
        self.runAction(seq, completion: { () -> Void in
            self.completedPlatformActions()
        })
        // get the reverse actions and initiate then on the scene / ground
        let reverseActions = self.getReverseMovements()
        let reverseSeq:SKAction = SKAction.sequence(reverseActions)
        delegate!.moveGroundWithPlatform(reverseSeq)
        self.isMoving = true
    }
}

然后,我有一个使用平台移动地面的功能,并向runAction添加一个键,以便在用户停止与平台联系时,我可以只停止该操作,而不是平台操作:

   func moveGroundWithPlatform(seq: SKAction) {
    [snip...]
    self.enumerateChildNodesWithName("platform") {
        node, stop in
        if let foundNode = node as? PlatformSprite {
            node.runAction(seq, withKey: "groundSeq")
        }
    }
  [snip...]
  }

然后我停止移动场景,但让平台的剩余动作继续使用:

    func stopMovingGroundWithPlatform() {
    [snip...]
    self.enumerateChildNodesWithName("platform") {
        node, stop in
        if let foundNode = node as? PlatformSprite {
            node.removeActionForKey("groundSeq")
        }
    }
    [snip...]
}

丑陋的我知道 - 如果其他人对如何更好地做到这一点有建议,我很想知道:)

如果你想让摩擦力让你

的角色随着平台一起移动,你需要用力、冲量或设置它的速度来移动平台。我怀疑通过设置平台的速度来控制平台更容易。您可以在update方法中通过以下方式执行此操作

platform.physicsBody?.velocity = CGVectorMake(dx,dy)

其中dxdy分别控制平台在 x 和 y 方向上的速度。您还应该设置平台物理体的摩擦属性。

最新更新