按住按钮时 SKAction 不是 运行

SKAction not running when button is held

我正在 working/practicing 使用 SpriteKit 并为控件制作方向键。它可以移动角色,但 SKAction 除外。我在 Actions.sks 文件中有动作,这些动作基于按下的按钮决定调用的动作。当我按下按钮时,角色移动得很好,但是当我按住它时,角色会滑动并且行走动画会卡在第一帧,直到我松开为止。我想要做的是在按下按钮时让角色移动,并在按住按钮时继续移动(带有行走动画)。我想让它看起来像 gameboy 时代的游戏。

class GameScene: SKScene {
    
    var player = SKSpriteNode()
    var playerSpeed: CGFloat = 0
    var previousTimeInterval:TimeInterval = 0
    let buttonNorth = SKSpriteNode(imageNamed: "Directional_Button")
    let buttonSouth = SKSpriteNode(imageNamed: "Directional_Button")
    let buttonEast = SKSpriteNode(imageNamed: "Directional_Button2")
    let buttonWest = SKSpriteNode(imageNamed: "Directional_Button2")
    var moveAtEndOfRelease:Bool = true
    var isPressing = false
    var currentState = MoveStates.n
    

    
    override func didMove(to view: SKView) {
        self.anchorPoint = CGPoint(x: 0.5, y: 0.5)

        // Make sure you can get teh player from the scene file
        if let somePlayer = self.childNode(withName: "player") as? SKSpriteNode {
            player = somePlayer
        
            // Set physics
            player.physicsBody?.isDynamic = false

        } else {
            print("No player")
        }
        
        
        let widthHalf:CGFloat = self.view!.bounds.width / 2
        let heightHalf:CGFloat = self.view!.bounds.height / 2
        
        
        self.addChild(buttonNorth)
        buttonNorth.position = CGPoint(x: -widthHalf + 80, y: -heightHalf + 100)
        
        self.addChild(buttonSouth)
        buttonSouth.position = CGPoint(x: -widthHalf + 80, y: -heightHalf + 40)
        buttonSouth.yScale = -1
        
        self.addChild(buttonWest)
        buttonWest.position = CGPoint( x: -widthHalf + 30, y: -heightHalf + 70)
        
        self.addChild(buttonEast)
        buttonEast.position = CGPoint( x: -widthHalf + 130, y: -heightHalf + 70)
        
        buttonNorth.xScale = 0.4
        buttonNorth.yScale = 0.4
        
        buttonSouth.xScale = 0.4
        buttonSouth.yScale = 0.4
        buttonSouth.zRotation = CGFloat(Double.pi)
        
        buttonEast.xScale = 0.4
        buttonEast.yScale = 0.4
        buttonEast.zRotation = CGFloat(Double.pi)
        
        buttonWest.xScale = 0.4
        buttonWest.yScale = 0.4
        
    }

    override func update(_ currentTime: TimeInterval) {
        
        //player.position = CGPoint(x: player.position.x + playerSpeedx, y: player.position.y + playerSpeedy)
        if (isPressing) {
            moveOnRelease()
        }
        
    }
    
    
    func move(facing: Facing, x: CGFloat, y: CGFloat) {
        let walkAnimation = SKAction(named: "walk\(facing)")!
        let moveAction = SKAction.moveBy(x: x, y: y, duration: 1)
        let group = SKAction.group([walkAnimation, moveAction])
        
        // Run the actions
        player.run(group)
        
    }
    
    func moveSide(facing: Facing, x: CGFloat) {
        let walkAnimation = SKAction(named: "walk\(facing)")!
        let moveAction = SKAction.moveBy(x: x, y: 0, duration: 1)
        let group = SKAction.group([walkAnimation, moveAction])
        
        // Run the actions
        player.run(group)
        
    }
    
    func moveUpDown(facing: Facing, y: CGFloat) {
        let walkAnimation = SKAction(named: "walk\(facing)")!
        let moveAction = SKAction.moveBy(x: 0, y: y, duration: 1)
        let group = SKAction.group([walkAnimation, moveAction])
        
        // Run the actions
        player.run(group)
        
    }
    
    func moveOnRelease() {
        
        switch (currentState) {
            
        case .n:
            moveUpDown(facing: .Back, y: 1)
        case .s:
            moveUpDown(facing: .Front, y: -1)
        case .e:
            moveSide(facing: .Right, x: 1)
        case .w:
            moveSide(facing: .Left, x: -1)
        
            
        }

    }
    
    
    override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        for touch in (touches ) {
            let location = touch.location(in: self)
            
          
            
            if (buttonNorth.frame.contains(location)) {
                
                currentState = MoveStates.n
                buttonNorth.texture = SKTexture(imageNamed: "Directional_Button_Lit")
                isPressing = true
                
            } else if (buttonSouth.frame.contains(location)) {
                
                currentState = MoveStates.s
                buttonSouth.texture = SKTexture(imageNamed: "Directional_Button_Lit")
                isPressing = true
                
            }  else if (buttonEast.frame.contains(location)) {
                
                currentState = MoveStates.e
                buttonEast.texture = SKTexture(imageNamed: "Directional_Button2_Lit")
                isPressing = true
                
            }  else if (buttonWest.frame.contains(location)) {
                
                currentState = MoveStates.w
                buttonWest.texture = SKTexture(imageNamed: "Directional_Button2_Lit")
                isPressing = true
                
            }
            
        }
    }
    
    

    
    override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
        
        if (moveAtEndOfRelease == true) {
            
            buttonNorth.texture = SKTexture(imageNamed: "Directional_Button")
            buttonSouth.texture = SKTexture(imageNamed: "Directional_Button")
            buttonEast.texture = SKTexture(imageNamed: "Directional_Button2")
            buttonWest.texture = SKTexture(imageNamed: "Directional_Button2")
            
            //moveOnRelease()
            isPressing = false
            
        }
    }

}

它卡在第一帧的原因是因为我相信每次更新都会调用该操作。您可以检查动画是否已经 运行ning,如果是,请不要再次 运行。

为此,请为每个 运行 操作命名。 所以 player.run(group) 可能是 player.run(group, withKey: "moveside")。然后,您可以检查该操作是否已经 运行ning 或不是这样 if (player.action(forKey: "moveside") == nil)。最后,添加代码以删除所有其他操作(如当前移动或动画)。

总而言之:

if (player.action(forKey: "moveside") == nil) {
    player.removeAllActions()
    player.run(group, withKey: "moveside")
}