SKAction动画无法正常工作-使用TLAnalogJoystick

时间:2019-07-14 18:40:45

标签: swift sprite-kit

我有一个TLAnalogJoystick类,在触地时将操纵杆放在屏幕上。我在函数中放置了一个SKAction.Animation,该函数应该随着操纵杆的移动来移动我的玩家。会发生移动,但是“动画”只能运行1次或仅在一个方向上运行。

我尝试过: -运行动画1次-因为经常调用moveJoystick.on(.move) -我尝试调用animateforever函数-这会导致播放器移动,并且当我松开操纵杆时它将永远运行 -我曾尝试在moveJoystick.on(.begin)中永久调用动画-这可行,但我无法更改动画的方向。 -可以立即使我的播放器永远向前移动,但是当我开始向右移动时,动画仍然显示他向前移动。

    enter code here
    override func didMove(to view: SKView) {
    var curPlayerDirection = playerDirection.forward
    playerNode = self.childNode(withName: "player") as? SKSpriteNode
    moveJoystick.handleImage = image
    moveJoystick.baseImage = image


    playerNode?.texture = forwardWalkingRightFoot

    /* Setup your scene here */
    backgroundColor = .green

    let moveJoystickHiddenArea = TLAnalogJoystickHiddenArea(rect:    CGRect(x: -960, y: -960, width: 2000, height: 2000))
    moveJoystickHiddenArea.zPosition = 5
    moveJoystickHiddenArea.joystick = moveJoystick
    moveJoystick.isMoveable = true
    addChild(moveJoystickHiddenArea)


    let walkForward = SKAction.animate(with: [self.forwardWalkingRightFoot, self.forwardWalkingLeftFoot], timePerFrame: walkSpeed)
    let walkLeft = SKAction.animate(with: [LeftWalkingLeftFoot, LeftWalkingRightFoot], timePerFrame: walkSpeed)
    let walkRight = SKAction.animate(with: [RightWalkingLeftFoot,RightWalkingRightFoot], timePerFrame: walkSpeed)
    let walkBack = SKAction.animate(with: [BackWalkingLeftFoot, BackWalkingRightFoot], timePerFrame: walkSpeed)

    let walkForeverForward = SKAction.repeatForever(walkForward)
    let walkForeverLeft = SKAction.repeatForever(walkLeft)
    let walkForeverRight = SKAction.repeatForever(walkRight)
    let walkForeverBack = SKAction.repeatForever(walkBack)

    moveJoystick.on(.begin) { [unowned self] _ in
        switch curPlayerDirection{
            case .forward:
                self.playerNode!.run(walkForeverForward)
            case .back:
                self.playerNode!.run(walkForeverBack)
            case .left:
                self.playerNode!.run(walkForeverLeft)
            case .right:
                self.playerNode!.run(walkForeverRight)
            }

        }
    moveJoystick.on(.move) { [unowned self] joystick in   
        guard let playerNode = self.playerNode else {
            return
        }
        let pVelocity = joystick.velocity;
        let playerDirectionX = pVelocity.x
        let playerDirectionY = pVelocity.y
        let speed = CGFloat(0.12)
        if (pVelocity.x >= 0.0) && (pVelocity.y >= 0.0){
            if playerDirectionX > playerDirectionY {
                playerNode.removeAllActions()
                playerNode.run(walkForeverRight)
                curPlayerDirection = playerDirection.right
            }
            else{
                playerNode.removeAllActions()
                playerNode.run(walkForeverBack)
                curPlayerDirection = playerDirection.back
            }
        }
        else if (pVelocity.x >= 0.0) && (pVelocity.y <= 0.0){
            if playerDirectionX > abs(playerDirectionY) {
                playerNode.removeAllActions()
                playerNode.run(walkForeverRight)
                curPlayerDirection = playerDirection.right
            }
            else{
                playerNode.removeAllActions()
                playerNode.run(walkForeverForward)
                curPlayerDirection = playerDirection.forward
            }
        }
        else if (pVelocity.x <= 0.0) && (pVelocity.y <= 0.0){
            if abs(playerDirectionX) > abs(playerDirectionY) {
                playerNode.removeAllActions()
                playerNode.run(walkForeverLeft)
                curPlayerDirection = playerDirection.left
            }
            else{
                playerNode.removeAllActions()
                playerNode.run(walkForeverForward)
                curPlayerDirection = playerDirection.forward
            }
        }
        else if (pVelocity.x <= 0.0) && (pVelocity.y >= 0.0){
            if abs(playerDirectionX) > playerDirectionY {
                playerNode.removeAllActions()
                playerNode.run(walkForeverLeft)
                curPlayerDirection = playerDirection.left
            }
            else{
                playerNode.removeAllActions()
                playerNode.run(walkForeverBack)
                curPlayerDirection = playerDirection.back
            }
        }
        playerNode.position = CGPoint(x: playerNode.position.x + (pVelocity.x * speed), y: playerNode.position.y + (pVelocity.y * speed))

    }
    moveJoystick.on(.end) { [unowned self] _ in

        self.playerNode!.removeAllActions()
    }

        //MARK: Handlers end

    view.isMultipleTouchEnabled = true
}

我的目标是为我的角色设置动画,使他看起来像他朝操纵杆控制运动的方向行走。我对控制器的其他建议持开放态度,但是TLAnalogJoystick就是我能找到的所有

0 个答案:

没有答案