SpriteKit 中的攻击按钮 [英] Attack button in SpriteKit

查看:20
本文介绍了SpriteKit 中的攻击按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Xcode 有点陌生,一直在为我的班级制作 2d 游戏.一段时间以来,我一直遇到按钮问题.我刚刚找到了为什么我的跳跃按钮不起作用的解决方案,但我还有一个攻击按钮.

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 button was not functioning, but I also have an attack button.

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

I have the code set up for the button to be on screen and to do its image change when pressed. However, I do not know what code to put in to change the player image to the jump animation. I also have no clue how to get collisions set up for it and the enemy so the attack animation will hit.

所以我的问题是,如何让按钮开始跳跃动画,以及如何设置碰撞以在被击中时摧毁敌人?

So my question is, how can I have the button start the jump animation and also how can I set up the collisions to destroy the enemy when hit?

这是我目前的代码:

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() {
}

推荐答案

你问题的前半部分很简单.您只需要在该函数中设置播放器的新纹理,然后在该动画完成后,恢复到原始播放器动画.所以我建议在类级别创建这两个动画,并为默认动画分配一个键,并使用攻击动作的完成处理程序将其设置回来.

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

至于关于碰撞的后半部分,您要使用的是contactTestBitmask.您将 SKPhysicsContactDelegate 添加到场景类中,并设置要在其上注册联系人的节点的位掩码.有很多关于这个方面的问题和答案.没有必要再回答我不认为.如果您对此感到困惑,请随时回来提出新问题.

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.

这篇关于SpriteKit 中的攻击按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆