一尘不染

SpriteKit中的“攻击”按钮

swift

我是Xcode的新手,并且正在为我的课程制作2D游戏。我已经有一段时间的按钮问题了。我刚刚有一个解决方案,说明为什么我的跳转按钮不起作用,但是我也有一个攻击按钮。

我已经为按钮设置了代码,使其可以在屏幕上显示,并在按下按钮时进行图像更改。但是,我不知道要插入什么代码才能将播放器图像更改为跳转动画。我也不知道如何为它和敌人设置碰撞,以使攻击动画能够命中。

所以我的问题是,如何让按钮启动跳跃动画,以及如何设置碰撞以击中敌人?

到目前为止,这是我的代码:

override func sceneDidLoad() {
    //Setup start button
    jumpButton = SKSpriteNode(texture: jumpButtonTexture)
    jumpButton.position = CGPoint(x: 1850, y: 130)
    jumpButton.size = CGSize(width: 200, height: 200)
    jumpButton.zPosition = 20

    addChild(jumpButton)

    //Setup start button
    attackButton = SKSpriteNode(texture: attackButtonTexture)
    attackButton.position = CGPoint(x: 1550, y: 130)
    attackButton.size = CGSize(width: 200, height: 200)
    attackButton.zPosition = 20

    addChild(attackButton)
}

override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        if selectedButton != nil {
            handleJumpButtonHover(isHovering: false)
            handleAttackButtonHover(isHovering: false)
        }

        if jumpButton.contains(touch.location(in: self)) {
            selectedButton = jumpButton
            handleJumpButtonHover(isHovering: true)
        } else if attackButton.contains(touch.location(in: self)) {
            selectedButton = attackButton
            handleAttackButtonHover(isHovering: true)
        }
    }
}

override func touchesEnded(_ touches: Set<UITouch>, with event: UIEvent?) {
    if let touch = touches.first {
        if selectedButton == jumpButton {
            handleJumpButtonHover(isHovering: false)

            if (jumpButton.contains(touch.location(in: self))) {
                handleJumpButtonClick()
            }
        } else if selectedButton == attackButton {
            handleAttackButtonHover(isHovering: false)

            if (attackButton.contains(touch.location(in: self))) {
                handleAttackButtonClick()
            }
        }
    }
    selectedButton = nil
    }

    func handleJumpButtonHover(isHovering : Bool) {
    if isHovering {
        jumpButton.texture = jumpButtonPressedTexture
    } else {
        jumpButton.texture = jumpButtonTexture
    }
}

func handleAttackButtonHover(isHovering : Bool) {
    if isHovering {
        attackButton.texture = attackButtonPressedTexture
    } else {
        attackButton.texture = attackButtonTexture
    }
}

func handleJumpButtonClick() {
    self.playerNode.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
    self.playerNode.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 20))
}

func handleAttackButtonClick() {
}

阅读 261

收藏
2020-07-07

共1个答案

一尘不染

您的问题的前半部分非常简单。您只需要在该功能中设置播放器的新纹理,然后在完成动画后将其还原为原始播放器动画。因此,我建议在类级别上创建这两个动画,并将键分配给默认动画,并使用攻击动作的完成处理程序将其设置回去。

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")
        })
    }
}

至于关于碰撞的后半部分,您想要使用contactTestBitmask。您可以将SKPhysicsContactDelegate添加到场景类,并设置要在其上注册联系人的节点的位掩码。整个SO中都有许多关于此方面的问题和答案。我认为无需再次回答。如果您挂了一个新问题,请随时回来。

2020-07-07