Animate Sprite左右移动由Player Swift控制

时间:2016-04-28 04:18:20

标签: ios swift sprite-kit

我有一个SpaceShooter游戏。我一直试图通过添加动画来让它看起来更好看。我目前在游戏中有一个boss战,我添加了大约17个循环的图像。

我设法循环播放器不受控制器控制的纹理,但是如何为玩家精灵添加纹理?

玩家在x轴上左右控制太空战斗机。我有5张想要使用的图像(左2,左1,中,右1,右2)

毫无疑问我必须使用switch case或if语句才能这样做但是我需要输入什么才能让xcode知道我希望sprite只在移动时切换并切换回它不向左或向右移动?

我之前尝试了一些东西,我把NSLogs只是为了看它是否输出" left"或"对"如果我向左或向右移动但它只是出于某种原因输出

以下是我对播放器的代码

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        /* Called when a touch begins */

        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)

            Player.position.x = location.x

        }
    }

        override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        var previousPosition: CGFloat = 0

        Player.position.x = location.x

        if Player.position.x > previousPosition
        {
            NSLog("Right")
            // add textures here
        }
        else if Player.position.x < previousPosition
        {
            NSLog("Left")
            // add textures here
        }
       previousPosition = Player.position.x

1 个答案:

答案 0 :(得分:0)

我很确定这会奏效。您所要做的就是将播放器的纹理更改为所需的纹理。

for touch: AnyObject in touches {
    let location = touch.locationInNode(self)
    var previousPosition: CGFloat = 0

    Player.position.x = location.x

    if Player.position.x > previousPosition
    {
        NSLog("Right")
        // add textures here
        Player.texture = SKTexture(imageNamed: "RightImage")
    }
    else if Player.position.x < previousPosition
    {
        NSLog("Left")
        // add textures here
        Player.texture = SKTexture(imageNamed: "LeftImage")
    }