Attack button in SpriteKit

前端 未结 2 1120
无人共我
无人共我 2020-12-04 00:29

I\'m slightly new to Xcode and have been making a 2d game for my class. I have been having issues with buttons for a while now. I had just got a solution to why my jump butt

2条回答
  •  借酒劲吻你
    2020-12-04 01:02

    The first half of your question is quite easy. You just need to set the new texture of your player in that function then once that animation is complete, revert back to the original player animation. So I would suggest creating those two animations at the class level and assigning a key to the default animation and use the completion handler of the attack action to set it back.

    class GameScene: SKScene {
        let frame2 = SKTexture(imageNamed: "Ttam2")
        let frame3 = SKTexture(imageNamed: "Ttam3")
        let frame4 = SKTexture(imageNamed: "Ttam4")
    
        let attackFrame1 = SKTexture(imageNamed: "Ttam1_ATTACK")
        let attackFrame2 = SKTexture(imageNamed: "Ttam2_ATTACK")
    
        var animation: SKAction!
        var attackAnination: SKAction!
    
        override func sceneDidLoad(){
           animation = SKAction.repeatForever(SKAction.animate(with: [playerTexture, frame2, frame3, frame4], timePerFrame: 0.2))
    
           attackAnimation = SKAction.animate(with: [attackFrame1,attackFrame2],timePerFrame: 0.2)
    
           playerNode.run(animation,withKey:"animate")
        }
    
        func handleAttackButtonClick(){
            playerNode.removeAction(forKey:"animate")
            playerNode.run(attackAnimation,completion:{
                 self.playerNode.run(animation,withKey: "animate")
            })
        }
    }
    

    As for your second half regarding collisions, what you want to use is contactTestBitmask. You add the SKPhysicsContactDelegate to your scene class and set the bitmasks of which nodes you want to register contacts on. There many questions and answers regarding that aspect all over SO. No need to answer again I don't think. Feel free to come back with a new question if you get hung up on it.

提交回复
热议问题