SpriteKit SKSpriteNode移动

时间:2016-07-22 00:19:58

标签: ios swift xcode sprite-kit

我有一些纹理......

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触摸纹理时 - 我的角色会跳到它上面。

请帮忙。

1 个答案:

答案 0 :(得分:2)

您可以通过检测要跳转的位置触摸来执行此操作,然后当用户触摸它时,您可以执行操作以将角色移动到那里。

要检测用户是否触摸要跳转的位置,可以将此代码添加到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])

希望这有助于