Home > Enterprise >  Why my sprite keeps flipping right when I move it left?
Why my sprite keeps flipping right when I move it left?

Time:04-10

I have a sprite that scales to 1 or -1 when moving right or left, if I move fast the finger it works fine, if I drag it slowly it doesn't stay in the left position and keeps flipping left and right. How can I solve it? This is how I handle the character movement:

override func touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) {
        let touch = touches.first
        if let location = touch?.location(in: self){
            player.run(SKAction.move(to: CGPoint(x:  playerPosition.x   location.x - startTouch.x, y: self.frame.height/2.5), duration: 0.1))
        }
        let touchLoc = touch!.location(in: self)
        let prevTouchLoc = touch!.previousLocation(in: self)
        
        let deltaX = touchLoc.x - prevTouchLoc.x
        player.position.x  = deltaX
        
        player.xScale = deltaX < 0 ? 1 : -1
    }

CodePudding user response:

//set x flip based on delta
if deltaX < 0 {
    player.xScale = -1
} else if deltaX > 0 {
    player.xScale = 1
}

Don’t change xScale if deltaX is 0

  • Related