ios - SpriteKit 播放器跳跃按钮

标签 ios swift button sprite-kit

我在将我的播放器连接到 SpriteKit 中的跳转按钮时遇到问题。我通过 Swift 文件创建了游戏。这是我目前所拥有的:

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.player.physicsBody?.velocity = CGVector(dx: 0, dy: 0)
    //self.player.physicsBody?.applyImpulse(CGVector(dx: 0, dy: 20))
}

func handleAttackButtonClick() {
}

到目前为止,handlejumpButtonClick 中的//self.player 应该允许角色跳起来,然后重力会把他拉下来。然而,截至目前,当按下跳跃按钮时,游戏崩溃并给出线程 1 exc 错误。

有谁知道让按钮与角色一起工作的方法吗?如果可能的话,有没有更短的方法可以做到这一点而不必重建游戏?

 func createPlayer(_ position: CGPoint) { 

    //graphic stuff
    let playerTexture = SKTexture(imageNamed: "Ttam1") //create variable with graphic assigned to it
    let player = SKSpriteNode(texture: playerTexture) //give player the graphic
    player.size = CGSize(width: 125, height: 200) //player sprite size
    player.zPosition = 20
    //physics stuff
    player.physicsBody = SKPhysicsBody(texture: playerTexture, size: player.size) //Set up pixel-perfect collision mesh for the player
    player.physicsBody?.isDynamic = true
    player.physicsBody!.affectedByGravity = true //Stop the player from being affected by gravity
    player.physicsBody?.allowsRotation = false

    //spawny stuff
    insertChild(player, at: 0) //spawn player
    player.position = position //move player to position passed into this method
    playerNode = player //Create a node where the player is, filled by the player itself... Like a D.Va ult! The call mech one, not the self-destruct one.

    let frame2 = SKTexture(imageNamed: "Ttam2")
    let frame3 = SKTexture(imageNamed: "Ttam3")
    let frame4 = SKTexture(imageNamed: "Ttam4")
    let animation = SKAction.animate(with: [playerTexture, frame2, frame3, frame4], timePerFrame: 0.2)
    let runForever = SKAction.repeatForever(animation)

    player.run(runForever)
}

这可能也是人们需要看到的东西(这也是来自 AnalogJoystick.swift 的代码):

let moveAnalogStick =  joystick(diameter: 240) //This is the size of the joystick on screen, probably in pixels


func createJoystick() {

    moveAnalogStick.position = CGPoint(x: moveAnalogStick.radius + 100, y: moveAnalogStick.radius + 5) //position of joystick on screen
    addChild(moveAnalogStick) //add joystick to screen

    moveAnalogStick.stick.color = UIColor.black //Joystick "stick" color
    //moveAnalogStick.stick.image = UIImage(named: "jStick") //Joystick "stick" graphic
    moveAnalogStick.substrate.color = UIColor.clear //Joystick "base" color
    moveAnalogStick.substrate.image = UIImage(named: "jSubstrate") //Joystick "base" graphic

    //MARK: Handlers begin
    moveAnalogStick.startHandler = { [unowned self] data in //These things happen only when the joystick is first pressed

        //CODE GOES HERE

    }

    moveAnalogStick.trackingHandler = { [unowned self] data in //These things happen while the joystick is being touched- regardless of movement. But honestly, this sucker is so sensitive, I'd give someone a medal if they can touch it without moving it at all

        //self.playerNode?.zRotation = (data.angular + 3.14159265359) //player rotation matches joystick, had to add in pi because of graphic facing wrong way- asked Jenny to fix this, but not a priority

        //these two lines are linked in a way I can't figure out how to untangle right now
        //They control movement though, I know that for a fact
        guard let pN = self.playerNode else { return }
        pN.position = CGPoint(x: pN.position.x + (data.velocity.x * 0.07), y: pN.position.y + (data.velocity.x * 0))

}

    moveAnalogStick.stopHandler = { [unowned self] data in //These things happen only when the joystick stops being pressed

        ///CODE GOES HERE

    }
    //MARK: Handlers end
}

最佳答案

我现在看到问题了。

您在 createPlayer() 函数中声明了 player 的局部变量,但将该播放器实例设置为全局变量 playerNode。但是当您施加冲动时,您正试图引用局部变量。将这两行改为

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

您可能正在获取 THREAD 1 EXC_BAD_INSTRUCTION。这通常意味着编译器并不完全知道您要做什么。换句话说,它知道变量 player 存在,但不能使用它。错误应该更具解释性。

关于ios - SpriteKit 播放器跳跃按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43597542/

相关文章:

ios - 即使用户重新安装后也限制 iOS 应用程序

ios - 我可以传入 Set<Int> 或 [Int] 的函数参数

java - Android - 添加按钮标题时的随机整数值

android - 如何将按钮的背景恢复为默认值(以编程方式)?

ios - 检查节点在屏幕上是否可见

ios - 当我们已经有一个单元格时尝试设置滑动以删除单元格....这看起来不太好

iOS swift 3 : Share Data from share extension to Host App

ios - Swift 2 中的 MPNowPlayingInfoCenter nowPlayingInfo 错误

html - HTML 中的多行按钮

swift - 使用相机录制的视频后如何播放?