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

时间:2017-10-04 20:01:17

标签: ios swift ontouchevent viewdidload touchesbegan

我必须按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")
        }
    }
}

在我的代码中,位置仅在触摸开始时打印一次,在您松开触摸并再次触摸之前不会打印。有没有办法用我的代码做到这一点?

4 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

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

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

答案 2 :(得分: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)")
    }
}

答案 3 :(得分: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
    }
}