我该如何改进动画代码?

时间:2018-03-27 12:54:49

标签: swift sprite-kit

屏幕上有2个按钮; moveLeft和moveRight。当我按下向左或向右按​​钮时,步行功能正常。但精灵似乎在滑动。我怎样才能改善这个?

像这样的精灵名字; player_walk_left_000,player_walk_left_001 ... 009 player_walk_right_000,player_right_left_02 ... 009

tapBegin(on button: String) {

        if button == "left" {
            walk(imagedNamed: "player_walk_left_00", direction: "left")
        } else if button == "right"{
           walk(imagedNamed: "player_walk_right_00", direction: "right")
        } else {
            //jump() I will develop here
        }
    }

tapEnd(on button:String) {
        if button == "left" {
            player.removeAction(forKey: "moveLeft")
        } else if button == "right" {
            player.removeAction(forKey: "moveRight")
        }
    }

func walk(imagedNamed: String, direction: String) {
    let playerWalk: SKAction
    var textures:[SKTexture] = []
    for i in 0...9 {
        textures.append(SKTexture(imageNamed: "\(imagedNamed)\(i)"))
    }
    textures.append(textures[2])
    textures.append(textures[1])
    if direction == "left" {
        playerWalk = SKAction.animate(with: textures,timePerFrame: 0.1)
        let leftForever = SKAction.repeatForever(playerWalk)
        let moveLeft = SKAction.moveBy(x: -60, y: 0, duration: 0.2)
        let moveLeftForEver = SKAction.repeatForever(moveLeft)
        player.run(SKAction.group([moveLeftForEver, leftForever]), withKey: "moveLeft")
    } else {
       playerWalk = SKAction.animate(with: textures,timePerFrame: 0.1)
        let rightForever = SKAction.repeatForever(playerWalk)
        let moveRight = SKAction.moveBy(x: 60, y: 0, duration: 0.2)
        let moveRightForEver = SKAction.repeatForever(moveRight)
        player.run(SKAction.group([moveRightForEver, rightForever]), withKey: "moveRight")
    }
}

0 个答案:

没有答案