【问题标题】:How to constantly get the x-position of an moving object in swift?如何不断快速获取移动物体的 x 位置?
【发布时间】:2017-10-04 20:01:16
【问题描述】:

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

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

【问题讨论】:

  • 您可以覆盖更新方法。在这种方法中,您应该能够获取您的对象并获取位置!
  • 我已经阅读了很多有关此更新方法的信息。我怎样才能覆盖它?当我尝试时,xCode 没有建议更新方法
  • 看看Bharath的答案!
  • 只在我移动手指时有效,但当我握住它而不移动时,船不断移动但位置只在开始时打印一次..
  • 也许你可以设置一个定时器,每0.1秒调用一个函数,这个函数,重置定时器,它会创建一个循环?当您移开手指时,您只需停止计时器

标签: ios swift touch-event viewdidload touchesbegan


【解决方案1】:

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

【讨论】:

  • 刚刚试过这个,它奏效了。有使用计时器的想法,但没有得到“选择器”位。谢谢!
【解决方案2】:

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

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

【讨论】:

  • 我试过了,当我移动手指时它确实打印了位置。但是当我按住左键并且不移动手指但按住按钮时,它不会不断打印x位置..
【解决方案3】:

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

【讨论】:

  • 听起来很不错。但试图实现它,它给了我leftButton没有成员“addGestureRecognizer()”的错误。我认为这是因为我的 leftButton 是 SKSpriteNode 而不是 Button。
【解决方案4】:

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

【讨论】:

    猜你喜欢
    • 2020-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-01
    • 2017-05-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多