【问题标题】:SpriteKit SKSpriteNode movesSpriteKit SKSpriteNode 移动
【发布时间】:2016-07-22 00:19:58
【问题描述】:

我有一些纹理...

placeForJump = SKSpriteNode(imageNamed: "placeForJump")
placeForJump.position = CGPoint(x: 512.346, y: 98.88)
placeForJump.size = CGSize(width: 166.407, height: 197.762)
placeForJump.zPosition = 2
self.addChild(placeForJump)

还有性格……

player = SKSPriteNode(imageNamed: "character")
player.position...
player.size...
player.zPozition = 3
self.addChild(player)

当我触摸到我可以跳跃的纹理时,我想让我的角色跳到这个地方,但我不知道该怎么做。

当我用 placeForJump 触摸纹理时 - 我的角色会跳上去。

请帮忙。

【问题讨论】:

    标签: ios swift xcode sprite-kit


    【解决方案1】:

    您可以通过检测跳跃位置上的触摸来做到这一点,然后当用户触摸它时,您可以运行一个动作将角色移动到那里。

    要检测用户是否触摸了跳转的地方,您可以将此代码添加到 touchesBegan 或 touchesEnded 函数:

    for touch in touches {
            let location = touch.locationInNode(self)
            if placeForJump.containsPoint(location) {
                 print("It was touched")
            }
    }
    

    然后在你想使用 SKAction 的地方运行一个动作,像这样:

    player.runAction(SKAction.moveTo(placeForJump.position, duration: speed)
    

    这会以最短的方式将玩家节点移动到 placeForJump,所以如果你想让它走得更高,然后到这个地方,你可以这样做:

    let highPoint = CGPoint(x: player.position.x + 50, y: player.position.y + 100)
    
    let moveUp = SKAction.moveTo(highPoint, duration: speed)
    let moveDown = SKAction.moveTo(placeForJump.position, duration: speed)
    player.runAction(SKAction.sequence([moveUp, moveDown])
    

    希望对你有帮助

    【讨论】:

    • 您好!你的回答真的很有帮助。非常感谢你。特别感谢这部分代码: let highPoint = CGPoint(x: player.position.x + 50, y: player.position.y + 100) let moveUp = SKAction.moveTo(highPoint, duration: speed) let moveDown = SKAction.moveTo(placeForJump.position, duration: speed) player.runAction(SKAction.sequence([moveUp, moveDown])
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多