2016-06-30 67 views
1

我在Swift和SpriteKit中創建了一個Side Scroller類似馬里奧的遊戲。我目前正在移動播放器,但你必須保持點擊,然後移動。我所希望的是,如果你堅持它,它會移動,你不必快速點擊屏幕。先謝謝你 !!!!連續觸摸/移動Swift和SpriteKit

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


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

        if location.y > 400{ 

      move = true 

     }else{ 


      if location.x < CGRectGetMidX(self.frame){ 
       player.physicsBody?.applyImpulse(CGVector(dx: -30, dy: 0)) 

       //tap somewhere above this to make character jump 
       if(location.y > 250) { 
        player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200)) 
       } 

      } else if location.x > CGRectGetMidX(self.frame){ 
       player.physicsBody?.applyImpulse(CGVector(dx: 30, dy: 0)) 

       //tap somewhere above this to make character jump 

      } 
     } 

    } 

    } 



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


    move = false 

} 
+0

看起來像設置一次移動,只移動一次你的對象?只要用戶沒有舉起手指,你想不斷調用它嗎? – Shripada

+0

是的像在JavaScript中有一個名爲** mouseIsPressed **的布爾值,這基本上是我需要的。 – ScarletStreak

回答

0

就個人而言,我會創建一個具有更新功能的控制器類,你輪詢您的場景更新以確定按鈕狀態是否爲開啓或關閉狀態(觸摸開始使按鈕處於關閉狀態,觸摸結束處於關閉狀態,更新輪詢狀態並根據狀態執行操作)但是在你的情況下,爲了保持簡單而不改變很多代碼,我只需要使用SKAction.moveBy

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


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

     if location.y > 400{ 

      move = true 

     }else{ 
      //tap somewhere above this to make character jump 
      if(location.y > 250) { 
       player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200)) 
      } 
      else 
      { 
       let direction = (location.x > CGRectGetMidX(self.frame)) ? 30 : -30 
       let move = SKAction.moveBy(x:direction,y:0,duration:0) 
       let rep = SKAction.repeatActionForever(move) 
       player.runAction(rep,forKey:"Moving") 
      } 
     } 
    } 
} 



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

    player.removeActionForKey("Moving") 
    move = false 

} 
+0

請注意,您需要處理多個觸摸,因此您可能還想要刪除touchbegin中的操作 – Knight0fDragon

0

我真的不很瞭解你怎麼想,但我嘗試寫一些代碼:

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     for touch: AnyObject in touches { 
      let location = touch.locationInNode(self) 
      if location.y > 400{ 
       move = true 
      }else{ 
       if location.x < CGRectGetMidX(self.frame){ 
        movePlayer(-30,dy:0) 

        //tap somewhere above this to make character jump 
        if(location.y > 250) { 
         player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 200)) 
        } 
       } else if location.x > CGRectGetMidX(self.frame){ 
        movePlayer(30,dy:0) 
        //tap somewhere above this to make character jump 
       } 
      } 
     } 
    } 
    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) { 
     for touch: AnyObject in touches { 
      let location = touch.locationInNode(self) 
      if location.y > 400{ 
       move = false 
      }else { 
       if location.x < CGRectGetMidX(self.frame) || if location.x > CGRectGetMidX(self.frame) { 
        movePlayer(0,dy:0) 
       } 
      } 
     } 
    } 

    func movePlayer(dx:CGFloat,dy:CGFloat) { 
     if (player.physicsBody.resting) { 
      player.physicsBody?.applyImpulse(CGVector(dx, dy: dy)) 
     } 
    }