如何在swift中不断获取移动物体的x位置?

How to constantly get the x-position of an moving object in swift?

我必须按钮 leftbutton, rightbutton 它们都是 SKSpriteNode()。 当用户触摸其中一个按钮时,只要用户按住触摸,就会有一艘小船左右移动。

现在我需要一个函数或任何其他能一直给我 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")
        }
    }
}

在我的代码中,该位置仅在触摸开始时打印一次,直到您释放触摸并再次触摸时才会打印。有没有办法用我的代码来做到这一点?

您好,您可以使用 touchesMoved 委托方法(它告诉响应者何时与事件关联的一个或多个触摸发生变化。)如下所示,

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

针对您在 Bharath answer 上发表的评论的解决方案。

您可以在下面更改为您自己的值:

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)")
    }
}

touchesMoved 函数无法帮助您解决特定问题。您可以通过创建一个 var timer = Timer() 作为实例变量来不断检查您的框架。

然后您必须设置计时器和在特定时间结束时调用的函数。 在 didMove 函数中执行以下操作:

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

由于这将每 0.01 秒重复一次,它将调用您将实现的函数 detectShipPosition OUTSIDE didMove

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

如果您在场景中实现 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
    }
}