Reanimation
Reanimation

Reputation: 3346

Small lag/jitter when tapping the screen

So my game is almost complete... but there's this little glitch or jitter that occurs when I press and hold my finger on the screen which, now I've noticed, I can't un-notice...

It happens really fast, and only happens when a function is called to handle tap&holds (long press). This happens after 0.2seconds have passed using a timer.

I've tried breakpointing it to pin down where exactly the jitter happens in the code, but it seems I can not fine tune it enough to locate it.

My update method is typical:

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
}

My function to handleLongPress is this:

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

The startPlayerAnimation_Sliding() just iterates a texture array of the 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")
}

Is there anything noticeable that may be causing this?

update

I've removed this from my update(..) method, and it seems smooth again... and I have no idea why...? Maybe because it's removing a key (explosion) that hasn't been created yet? or the fact it's removing these keys every frame... Doesn't make sense though... But I'm calling it a night, and looking at this again tomorrow. Thanks for your help so far. Have a good evening. (will update tomorrow)

    //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
    }

Upvotes: 0

Views: 212

Answers (1)

Knight0fDragon
Knight0fDragon

Reputation: 16847

Yikes, how you create textures is what is slowing you down a lot, you are creating new textures every time a touch happens, this is not needed. Instead do:

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

Upvotes: 1

Related Questions