-1
\$\begingroup\$

I've added buttons and SKCameraNode for my player. I got problem. When player moving left/right/jump all buttons dont follow for him.

UPDATE

class GameScene: SKScene, SKPhysicsContactDelegate {

var player: SKSpriteNode?

let buttonDirUp = SKSpriteNode (imageNamed: "button_dir_up")
let buttonDirLeft = SKSpriteNode (imageNamed: "button_dir_left")
let buttonDirDown = SKSpriteNode (imageNamed: "button_dir_down")
let buttonDirRight = SKSpriteNode (imageNamed: "button_dir_right")

var pressedButtons = [SKSpriteNode]()

override func didMoveToView(view: SKView) {

    physicsWorld.contactDelegate = self

    self.physicsWorld.gravity = CGVectorMake(0, -5)

    userInteractionEnabled = true



    player = self.childNodeWithName("player") as? SKSpriteNode

    buttonDirUp.position = CGPoint(x: 100, y: 150)
    buttonDirUp.zPosition = 2
    buttonDirUp.setScale(2.0)
    buttonDirUp.alpha = 0.2
    self.addChild(buttonDirUp)

    buttonDirLeft.position = CGPoint(x: 50, y: 100)
    buttonDirLeft.zPosition = 2
    buttonDirLeft.setScale(2.0)
    buttonDirLeft.alpha = 0.2
    self.addChild(buttonDirLeft)

    buttonDirDown.position = CGPoint(x: 100, y: 50)
    buttonDirDown.zPosition = 2
    buttonDirDown.setScale(2.0)
    buttonDirDown.alpha = 0.2
    self.addChild(buttonDirDown)

    buttonDirRight.position = CGPoint(x: 150, y: 100)
    buttonDirRight.zPosition = 2
    buttonDirRight.setScale(2.0)
    buttonDirRight.alpha = 0.2
    self.addChild(buttonDirRight)
}

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {

        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)

            for button in [buttonDirUp, buttonDirLeft, buttonDirDown, buttonDirRight] {

                if button.containsPoint(location) && pressedButtons.indexOf(button) == nil {
                    pressedButtons.append(button)
                }
            }
        }

        for button in [buttonDirUp, buttonDirLeft, buttonDirDown, buttonDirRight] {
            if pressedButtons.indexOf(button) == nil {
                button.alpha = 0.2
            }
            else {
                button.alpha = 0.8
            }
        }
    }
    override func touchesMoved(touches: Set<UITouch>, withEvent event: UIEvent?) {

        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            let previousLocation = touch.previousLocationInNode(self)

            for button in [buttonDirUp, buttonDirLeft, buttonDirDown, buttonDirRight] {

                if button.containsPoint(previousLocation)
                    && !button.containsPoint(location) {

                        let index = pressedButtons.indexOf(button)
                        if index != nil {
                            pressedButtons.removeAtIndex(index!)
                        }
                }

                else if !button.containsPoint(previousLocation)
                    && button.containsPoint(location)
                    && pressedButtons.indexOf(button) == nil {
                        pressedButtons.append(button)
                }
            }
        }

        for button in [buttonDirUp, buttonDirLeft, buttonDirDown, buttonDirRight] {
            if pressedButtons.indexOf(button) == nil {
                button.alpha = 0.2
            }
            else {
                button.alpha = 0.8
            }
        }
    }

    override func touchesEnded(touches: Set<UITouch>, withEvent event: UIEvent?) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            let previousLocation = touch.previousLocationInNode(self)

            for button in [buttonDirUp, buttonDirLeft, buttonDirDown, buttonDirRight] {
                if button.containsPoint(location) {
                    let index = pressedButtons.indexOf(button)
                    if index != nil {
                        pressedButtons.removeAtIndex(index!)
                    }
                }
                else if (button.containsPoint(previousLocation)) {
                    let index = pressedButtons.indexOf(button)
                    if index != nil {
                        pressedButtons.removeAtIndex(index!)
                    }
                }
            }
        }
        for button in [buttonDirUp, buttonDirLeft, buttonDirDown, buttonDirRight] {
            if pressedButtons.indexOf(button) == nil {
                button.alpha = 0.2
            }
            else {
                button.alpha = 0.8
            }
        }
    }



override func touchesCancelled(touches: Set<UITouch>?, withEvent event: UIEvent?) {

    for touch: AnyObject in touches! {
        let location = touch.locationInNode(self)
        let previousLocation = touch.previousLocationInNode(self)

        for button in [buttonDirUp, buttonDirLeft, buttonDirDown, buttonDirRight] {
            if button.containsPoint(location) {
                let index = pressedButtons.indexOf(button)
                if index != nil {
                    pressedButtons.removeAtIndex(index!)
                }
            }
            else if (button.containsPoint(previousLocation)) {
                let index = pressedButtons.indexOf(button)
                if index != nil {
                    pressedButtons.removeAtIndex(index!)
                }
            }
        }
    }
    for button in [buttonDirUp, buttonDirLeft, buttonDirDown, buttonDirRight] {
        if pressedButtons.indexOf(button) == nil {
            button.alpha = 0.2
        }
        else {
            button.alpha = 0.8
        }
    }
}



override func update(currentTime: CFTimeInterval) {
            if pressedButtons.indexOf(buttonDirUp) != nil {
        player!.position.y += 9.0
    }
    if pressedButtons.indexOf(buttonDirDown) != nil {
        player!.position.y -= 9.0
    }
    if pressedButtons.indexOf(buttonDirLeft) != nil {
        player!.position.x -= 9.0
    }
    if pressedButtons.indexOf(buttonDirRight) != nil {
        player!.position.x += 9.0
    }
}

}

\$\endgroup\$
4
  • \$\begingroup\$ We need more information to be able to help you, maybe show us some code (If you have, I don´t know how that tool works), if not, maybe some screenshots? \$\endgroup\$ Commented Feb 3, 2016 at 11:47
  • \$\begingroup\$ @Mayuso Sorry. I've added code. \$\endgroup\$ Commented Feb 4, 2016 at 12:27
  • \$\begingroup\$ @Mayuso i thought about SkCameraNode fot buttons. No? \$\endgroup\$ Commented Feb 5, 2016 at 8:16
  • \$\begingroup\$ Why do you mention SKCameraNode? I don't see you using that anywhere in your code. \$\endgroup\$ Commented Feb 12, 2016 at 17:58

1 Answer 1

0
\$\begingroup\$

You could add the buttons as a child of the player. Instead of:

self.addChild(buttonDirUp)

it would be

player.addChild(buttonDirUp)
\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.