Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在不拖动的情况下轻弹/轻扫精灵?_Ios_Swift_Sprite Kit_Game Physics - Fatal编程技术网

Ios 如何在不拖动的情况下轻弹/轻扫精灵?

Ios 如何在不拖动的情况下轻弹/轻扫精灵?,ios,swift,sprite-kit,game-physics,Ios,Swift,Sprite Kit,Game Physics,现在在我的游戏中,我可以拖动、滑动和轻弹节点。但是,我希望用户只能滑动/轻弹节点,而不希望用户能够拖动节点。我该怎么做 这是我目前的代码: override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { for touch in touches { let location = touch.location(in: self) if ball.frame

现在在我的游戏中,我可以拖动、滑动和轻弹节点。但是,我希望用户只能滑动/轻弹节点,而不希望用户能够拖动节点。我该怎么做

这是我目前的代码:

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {

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

        if ball.frame.contains(location) {
            touchPoint = location
            touching = true
        }
    }
}

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
    let touch = touches.first as UITouch!
    let location = touch!.location(in: self)
    touchPoint = location

    for touch in touches {
        let location = touch.location(in: self)
        touchPoint = location
        if touching {
            if touchPoint != ball.position {
                let dt:CGFloat = 1.0/60.0
                let distance = CGVector(dx: touchPoint.x-ball.position.x, dy: touchPoint.y-ball.position.y)
                let velocity = CGVector(dx: distance.dx/dt, dy: distance.dy/dt)
                ball.physicsBody!.velocity=velocity
            }
        }   
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    touching = false

    for touch in touches {
        let location = touch.location(in: self)
        touchPoint = location
        let node = self.atPoint(location)
    }
}
override func touchsbegind(touch:Set,带有事件:UIEvent?){
接触{
让位置=触摸。位置(in:self)
如果ball.frame.contains(位置){
接触点=位置
触摸=真实
}
}
}
覆盖功能触摸移动(touchs:Set,带有事件:UIEvent?){
让触摸=触摸。先触摸!
让位置=触摸!.location(在:self中)
接触点=位置
接触{
让位置=触摸。位置(in:self)
接触点=位置
如果接触{
如果接触点!=球位置{
设dt:CGFloat=1.0/60.0
让距离=CGVector(dx:touchPoint.x-ball.position.x,dy:touchPoint.y-ball.position.y)
设速度=CGVector(dx:distance.dx/dt,dy:distance.dy/dt)
ball.physicsBody!。速度=速度
}
}   
}
}
覆盖函数touchesend(touchs:Set,带有事件:UIEvent?){
触摸=错误
接触{
让位置=触摸。位置(in:self)
接触点=位置
让节点=self.atPoint(位置)
}
}

这就是为什么存在
UIgestureRecognitors
的原因。在视图上添加一个
uipangesturecognizer
,并使用
velocity(in:UIView)
方法让您知道轻弹的方向和速度

请注意,您不能将手势放在节点上,只能放在视图上,因此如果您只使用手势,则需要将视图坐标转换为场景坐标

您还可以允许手势的触控向下渗透到场景,以允许正常的触控呼叫,并且只使用平移手势的速度


请更清楚地定义您想要实现的目标。我猜:您的目标是让节点在手指从滑动手势中释放出来之前不移动,此时手指在屏幕上的移动速率和距离决定了节点的移动速率和方向?