首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SKSpriteNode平台使SKSpriteNode角色移动

使用SKSpriteNode平台使SKSpriteNode角色移动
EN

Stack Overflow用户
提问于 2014-11-26 16:19:27
回答 1查看 907关注 0票数 1

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

当平台移动时,角色不会移动,最终会从平台上掉下来。如果我点击移动按钮,角色会移动得很好,但我想让角色在移动时‘粘’在平台上。

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

编辑:

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

当用户按下向左或向右方向按钮时,我将场景从GameScene.swift移动到:

代码语言:javascript
复制
    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发送反向序列来启动场景移动:

代码语言:javascript
复制
    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添加一个键,这样如果用户停止与平台的联系,我就可以停止该操作,而不是平台的操作:

代码语言:javascript
复制
   func moveGroundWithPlatform(seq: SKAction) {

    [snip...]

    self.enumerateChildNodesWithName("platform") {
        node, stop in

        if let foundNode = node as? PlatformSprite {
            node.runAction(seq, withKey: "groundSeq")
        }

    }
  [snip...]
  }

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

代码语言:javascript
复制
    func stopMovingGroundWithPlatform() {

    [snip...]
    self.enumerateChildNodesWithName("platform") {
        node, stop in

        if let foundNode = node as? PlatformSprite {
            node.removeActionForKey("groundSeq")
        }

    }
    [snip...]
}

丑陋的我知道-如果其他人有关于如何做得更好的建议,我很想知道:)

EN

回答 1

Stack Overflow用户

发布于 2014-11-26 18:27:47

如果希望摩擦力使角色与平台一起移动,则需要使用力、冲量或通过设置其速度来移动平台。我怀疑通过设置速度来控制平台会更容易一些。您可以通过以下方式在update方法中完成此操作

代码语言:javascript
复制
platform.physicsBody?.velocity = CGVectorMake(dx,dy)

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27144209

复制
相关文章

相似问题

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