2017-10-04 88 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 
    } 
}