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

查看:118
本文介绍了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?

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

This is my code so far:

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

As关于碰撞的下半部分,你想要使用的是 contactTestBitmask 。您将SKPhysicsContactDelegate添加到场景类,并设置要在其上注册联系人的节点的位掩码。 SO的这方面有很多问题和答案。没有必要再回答我不认为。如果你被挂断了,请随时回来一个新问题。

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天全站免登陆