2016-04-28 38 views
0

我有SpaceShooter游戏。我一直试图通过添加动画来让它看起来更好。我目前在游戏中有一场老大战,我添加了大约17张循环播放的图片。动画Sprite左右移动由Player Swift控制

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

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

毫无疑问,我将不得不使用switch case或if语句来做到这一点,但我需要输入什么让xcode知道我只希望精灵在移动时切换并切换回不移动左侧或右侧?

我试过的东西前面,我把NSLogs只是为了看看它输出“左”或“右”如果我把它向左或向右,但它只是输出正确出于某种原因

这里是我的代码对于球员有

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 
+0

'previousPosition'是一个局部变量,在每次调用时总是初始化为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") 
    }