Sprite kit spritekit/ios中手指在精灵上的连续最小速度跟踪

Sprite kit spritekit/ios中手指在精灵上的连续最小速度跟踪,sprite-kit,sprite,uigesturerecognizer,swipe,gesture,Sprite Kit,Sprite,Uigesturerecognizer,Swipe,Gesture,在不举起手指的情况下,连续跟踪用户在精灵上上下移动手指的速度的好方法是什么?如果他们没有足够快(设置最小值)来执行某个操作。这是一些代码,您可以根据手指加速度以不同的速度在屏幕上拖动精灵。您可以添加自己的代码来检测更改的大小,并执行您想要的任何操作 class游戏场景:SKScene{ //时间值 变量增量:NSTimeInterval=NSTimeInterval(0) var上次更新时间:NSTimeInterval=NSTimeInterval(0) //我们的精灵 设sprite=SK

在不举起手指的情况下,连续跟踪用户在精灵上上下移动手指的速度的好方法是什么?如果他们没有足够快(设置最小值)来执行某个操作。

这是一些代码,您可以根据手指加速度以不同的速度在屏幕上拖动精灵。您可以添加自己的代码来检测更改的大小,并执行您想要的任何操作

class游戏场景:SKScene{
//时间值
变量增量:NSTimeInterval=NSTimeInterval(0)
var上次更新时间:NSTimeInterval=NSTimeInterval(0)
//我们的精灵
设sprite=SKSpriteNode(颜色:SKColor.redColor(),大小:CGSize(宽度:20,高度:30))
//跟踪触摸之间的差异
var currentTouchY:CGFloat=CGFloat(0)
var lastTouchY:CGFloat=CGFloat(0)
//当前和最后一次触摸之间的变化幅度
变量forceMag:CGFloat=CGFloat(0)
//max mag这样我们的精灵就不会以疯狂的速度旅行了
设forceMagMax:CGFloat=CGFloat(7)
//我们精灵旅行的方向
变量forceDir:CGFloat=CGFloat(0)
//任意速度我们希望精灵移动
让速度:CGFloat=CGFloat(70)
//当我们放手时,我们用这个来停止移动我们的精灵
var intervalsWithoutChange=0
覆盖func didMoveToView(视图:SKView){
sprite.position.x=100
sprite.position.y=size.width/2
self.addChild(精灵)
}
覆盖功能触摸开始(触摸:设置,withEvent事件:UIEvent?){
用于触摸:触摸中的任何对象{
let location=touch.locationInNode(自)
设yCoord=location.y
lastTouchY=yCoord
}
}
覆盖功能触摸移动(触摸:设置,带事件:UIEvent?){
用于触摸:触摸中的任何对象{
let location=touch.locationInNode(自)
设yCoord=location.y
未更改的间隔=0
currentTouchY=yCoord
设diff=currentTouchY-lastTouchY
forceMag=abs(差异)
forceMag=forceMag>forceMagMax?forceMagMax:forceMag
forceDir=diff<0?-1:1
lastTouchY=yCoord
}
}
覆盖func touchesEnded(触摸:设置,withEvent事件:UIEvent?){
未更改的间隔=1
currentTouchY=0
lastTouchY=0
}
覆盖函数更新(当前时间:NSTimeInterval){
如果上次更新时间=0.0{
增量=0
}否则{
增量=当前时间-上次更新时间
}
上次更新时间=当前时间
如果intervalsWithoutChange>0{
forceMag-=CGFloat(15)*CGFloat(δ)
}
如果forceMag<0.01{
forceMag=0
}
让变化=力量方向*力量(力量方向,1.29)
sprite.position.y+=变化*CGFloat(增量)*速度
}
}
class GameScene: SKScene {

    // time values
    var delta:NSTimeInterval = NSTimeInterval(0)
    var last_update_time:NSTimeInterval = NSTimeInterval(0)

    // our sprite
    let sprite = SKSpriteNode(color: SKColor.redColor(), size: CGSize(width: 20, height: 30))

    // tracking difference between touches
    var currentTouchY: CGFloat = CGFloat(0)
    var lastTouchY: CGFloat = CGFloat(0)

    // the magnitude of change between current and last touch
    var forceMag: CGFloat = CGFloat(0)
    // max mag so our sprite doesnt travel at crazy speed
    let forceMagMax: CGFloat = CGFloat(7)
    // direction of our sprites travel
    var forceDir: CGFloat = CGFloat(0)
    // arbitrary speed we want sprite to move
    let velocity: CGFloat = CGFloat(70)

    // we use this to stop moving our sprite when we let go
    var intervalsWithoutChange = 0

    override func didMoveToView(view: SKView) {
        sprite.position.x = 100
        sprite.position.y = size.width/2
        self.addChild(sprite)
    }

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            let yCoord = location.y

            lastTouchY = yCoord
        }
    }

    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            let yCoord = location.y

            intervalsWithoutChange = 0
            currentTouchY = yCoord
            let diff = currentTouchY - lastTouchY
            forceMag = abs(diff)
            forceMag = forceMag > forceMagMax ? forceMagMax : forceMag
            forceDir = diff < 0 ? -1 : 1
            lastTouchY = yCoord
        }
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        intervalsWithoutChange = 1
        currentTouchY = 0
        lastTouchY = 0
    }

    override func update(currentTime: NSTimeInterval) {

        if last_update_time == 0.0 {
            delta = 0
        } else {
            delta = currentTime - last_update_time
        }

        last_update_time = currentTime

        if intervalsWithoutChange > 0 {
            forceMag -= CGFloat(15) * CGFloat(delta)
        }

        if forceMag < 0.01 {
            forceMag = 0
        }

        let change = forceDir * pow(forceMag, 1.29)

        sprite.position.y += change * CGFloat(delta) * velocity
    }
}