点击屏幕时小 lag/jitter

Small lag/jitter when tapping the screen

所以我的游戏几乎完成了...但是当我将手指按住屏幕时会出现这种小故障或抖动,现在我已经注意到了,我无法不注意到... .

它发生得非常快,并且只有在调用一个函数来处理点击和按住(长按)时才会发生。这在使用计时器经过 0.2 秒后发生。

我已经尝试对其设置断点以确定代码中抖动的确切位置,但似乎我无法对其进行微调以找到它。

我的更新方式是典型的:

override func update(currentTime: CFTimeInterval) {

    //calc delta time
    if lastUpdateTime > 0 {
        dt = currentTime - lastUpdateTime
    } else {
        dt = 0
    }
    lastUpdateTime = currentTime

    //timer for handleLongPress
    if touched {
        longTouchTimer += dt
    }

    if longTouchTimer >= 0.2 && !canLongPressNow {
        canLongPressNow = true
        handleLongPress()
    } else {
        canLongPressNow = false
    }

    ...
    //switch GameSate
    //switch CharacterState
}

我处理长按的函数是这样的:

func handleLongPress() {
    //switch gameState
    //if in gameplay gamestate
        if canLongPressNow {
            //rotate player
            //change character state
            startPlayerAnimation_Sliding()
        }
    touched = false
    longTouchTimer = 0
}

startPlayerAnimation_Sliding()只是迭代playerNode的一个纹理数组。

func startPlayerAnimation_Sliding() {
   var textures: Array<SKTexture> = []
    for i in 0..<KNumSlideFrames{
        textures.append(SKTexture(imageNamed: "slide\(i)"))
    }
    let playerAnimation = SKAction.animateWithTextures(textures, timePerFrame: 0.3)
    player.runAction(SKAction.repeatActionForever(playerAnimation), withKey: "sliding")
}

是否有任何明显的原因可能导致此问题?

更新

我已经从我的 update(..) 方法中删除了它,它看起来又很顺利了……我不知道为什么……?也许是因为它正在删除尚未创建的密钥(爆炸)?或者它每帧都删除这些键的事实......虽然没有意义......但我称之为晚上,明天再看这个。感谢您一直以来的帮助。晚上好。 (明天更新)

    //for animations
    switch characterState {
        case .Running:
            player.removeActionForKey("exploding")
            player.removeActionForKey("sliding")
            break
        case .Sliding:
            player.removeActionForKey("running")
            player.removeActionForKey("exploding")
            break
        case .Exploding:
            player.removeActionForKey("running")
            player.removeActionForKey("sliding")
            break
    }

哎呀,你创建纹理的方式让你的速度变慢了很多,每次触摸发生时你都在创建新的纹理,这是不需要的。相反:

var textures: Array<SKTexture> = []
var playerAnimation : SKAction?
func loadingPhase()  //however this is defined for you
{
    for i in 0..<KNumSlideFrames{
        textures.append(SKTexture(imageNamed: "slide\(i)"))
    }
    playerAnimation = SKAction.repeatActionForever(SKAction.animateWithTextures(textures, timePerFrame: 0.3))

}
func startPlayerAnimation_Sliding() {


    player.runAction(playerAnimation!, withKey: "sliding")
}