需要一个向右和向左按钮来移动x轴上的精灵

时间:2016-12-24 06:26:51

标签: swift sprite-kit swift3 skspritenode

我已经搜索过,我只能通过翻转/拖动来查找有关移动的信息。我是XcodeSprite-Kit制作游戏的初学者,到目前为止,我的表现相当不错,比诚实的要好。

但我似乎无法弄清楚如何在按下左右按钮时左右移动精灵。我左边SKSpriteNode和右边SKSpriteNode(我不认为这是正确的),它们显示我想要的地方,但我不知道如何使它们被触摸并使精灵移动。

对不起,如果这是一个非常业余的问题,答案可能会很明显,但是我还是在堵塞。

1 个答案:

答案 0 :(得分:3)

有很多关于移动精灵的教程,从这个raywenderlich教程开始。 您必须为精灵设置名称并在touchesBegan函数中处理移动(使用此thread

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    for touch in (touches) {
        let positionInScene = touch.location(in: self)
        let touchedNode = self.atPoint(positionInScene)
        if let name = touchedNode.name {
            if name == "leftButton" {
                //move to left
            }
            if name == "rightButton" {
                //move to right
               let moveAction: SKAction = SKAction.moveBy(x: 15, y: 0, duration: 1)
               yourNode.run(moveAction)
            }
        }
    }
}