SpriteKit播放器跳转按钮

时间:2017-04-24 20:54:05

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

到目前为止,//self.player中的handlejumpButtonClick应该允许角色跳起来然后引力会让他退缩。但是,截至目前,当按下跳转按钮时,游戏崩溃并给出一个线程1错误。

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

 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
}

1 个答案:

答案 0 :(得分:1)

我现在看到了问题。

您在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存在,但它无法使用它。错误应该更加明确。