2017-10-04 91 views
0

我必須按leftbutton, rightbutton他們都是SKSpriteNode()。 當用戶觸摸其中一個按鈕時,只要用戶持續觸摸,就會有一小船左右移動。如何不斷快速獲取移動物體的x位置?

現在我需要給我的ship.position.x整個時間的函數或其他任何東西。我堅持要讓它不斷打印位置。每次觸摸按鈕時我都可以打印,但只打印一次。

在我didMove我只創建了按鈕和船舶。所以它應該是無關緊要的。

func moveShip (moveBy: CGFloat, forTheKey: String) { 
    let moveAction = SKAction.moveBy(x: moveBy, y: 0, duration: 0.09) 
    let repeatForEver = SKAction.repeatForever(moveAction) 
    let movingSequence = SKAction.sequence([moveAction, repeatForEver]) 
    ship.run(movingSequence, withKey: forTheKey) 
} 

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 
    print("\(ship.position.x)") 

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

     if leftButton.contains(pointTouched) { 

//   !! I MAKE IT PRINT THE POSITION HERE !! 

      moveShip(moveBy: -30, forTheKey: "leftButton") 
     } 

     else if rightButton.contains(pointTouched) { 
      moveShip(moveBy: 30, forTheKey: "rightButton") 
     } 
    } 
} 

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) { 
    if let touch = touches.first{ 
     let pos = touch.location(in: self) 
     let node = self.atPoint(pos) 

     if node == aButton { 

     } else { 
      ship.removeAction(forKey: "leftButton") 
      ship.removeAction(forKey: "rightButton") 
     } 
    } 
} 

在我的代碼,該位置僅在觸摸的開始打印一次,但不打印,直到你再次釋放觸摸觸摸它。有沒有可能的方式來做到這一點與我的代碼?

+0

您可以覆蓋更新方法。在這種方法中,您應該能夠獲得您的對象並獲得位置! –

+0

我讀了很多關於這種更新方法。我如何覆蓋它?當我嘗試,沒有更新方法建議xCode –

+0

看看Bharath的答案! –

回答

2

touchesMoved功能不會幫助你與你的特定問題。你可以通過創建一個var timer = Timer()作爲實例變量來持續檢查你的幀。

然後您必須設置計時器和特定時間結束時調用的函數。 請在您的didMove功能如下:

timer = Timer.scheduledTimer(timeInterval: 0.01, target: self, 
     selector: #selector(detectShipPosition), userInfo: nil, repeats: true) 

由於這將重演每0.01秒鐘,這兩點會調用該函數detectShipPosition你將實現OUTSIDEdidMove

func detectShipPosition(){ 
    print("\(ship.position.x)") 
} 
+1

只是試過這個,它的工作。有想法使用計時器,但沒有得到「選擇器」位。謝謝! –

1

嗨,你可以使用的touchesMoved委託方法(它告訴響應時,與事件相關的一個或多個觸摸更改。)如下,供您發佈的評論

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) { 
     print("\(ship.position.x)") 
    } 
+0

我試過這個,當我移動手指時它確實打印了位置。但是當我按住左按鈕,不要移動手指,但按住按鈕時,它不會持續打印x位置。 –

1

解決方案Bharath回答。

您可以用您自己的值以下變化:

longGesture.minimumPressDuration 

您可以使用UILongPressGestureRecognizer

class ViewController: UIViewController, UIGestureRecognizerDelegate { 
    @IBOutlet weak var leftButton: UIButton! 
    @IBOutlet weak var rightButton: UIButton! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let longGesture = UILongPressGestureRecognizer(target: self, action: #selector(ViewController.longPress(_:))) 
     longGesture.minimumPressDuration = 0.5 
     longGesture.delegate = self 
     leftButton.addGestureRecognizer(longGesture) 

     rightButton.addGestureRecognizer(longGesture) 

    } 

    @objc func longPress(_ gestureReconizer: UILongPressGestureRecognizer) { 
     print("\(ship.position.x)") 
    } 
} 
+0

聽起來真的很不錯。但試圖實現它,它給了我leftButton沒有成員「addGestureRecognizer()」的錯誤。我認爲這是因爲我的leftButton是一個SKSpriteNode而不是Button。 –

0

如果要實現在場景中的update()函數,它將在那裏被調用的每一幀,你可以檢查你的對象的位置(和存在)打印值時,它改變:

override func update(_ currentTime: TimeInterval) 
{ 
    if let ship = theShipSprite, 
     ship.position != lastPosition 
    { 
     print(ship.position) // or whatever it is you need to do 
     lastPosition = shipPosition 
    } 
}