Lucas Farleigh
Lucas Farleigh

Reputation: 19

My Sprite Kit button won't work in Swift

Hi than you for looking at this i was wondering why my code in swift sprite kit won't work i can run it but when i click on the button it does not work(it does not go to the scene) but when i add a println to the if statement and then i click it it does run the println. Thank you in advance!

Here is my code for my first scene:

    //
//  GameScene.swift
//  Pocket Rocket
//
//  Created by Lucas Farleigh on 11/11/2014.
//  Copyright (c) 2014 Lucas Farleigh. All rights reserved.
//

import SpriteKit;

class GameScene: SKScene {

    let background = SKSpriteNode(imageNamed:"background")
    let playButton = SKSpriteNode(imageNamed: "playbutton")
    override func didMoveToView(view: SKView) {
        playButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidX(self.frame))
        background.yScale = 2
        background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidX(self.frame))
    self.addChild(background)
        self.addChild(playButton)

    }
    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        /* Called when a touch begins */
        //making the scene vars
        var scene =  PlayScene(size: self.size)


        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
             let node = self.nodeAtPoint(location)
            let skview = self.view as  SKView!
            scene.size = skview.bounds.size
            playButton.name = "PB"


            if node.name == "PB"{
                println("it worked")
                skview.presentScene(scene)
            }


        }
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

And my second scene(playscene)

    //
//  PlayScene.swift
//  Pocket Rocket
//
//  Created by Lucas Farleigh on 14/11/2014.
//  Copyright (c) 2014 Lucas Farleigh. All rights reserved.
//

import SpriteKit
import UIKit
import Foundation

class PlayScene:SKScene{
    let background = SKSpriteNode(imageNamed: "background")

    let bar1 = SKSpriteNode(imageNamed:"Bar1")


    let bar2 = SKSpriteNode(imageNamed:"Bar2")

    let bar3 = SKSpriteNode(imageNamed:"Bar3")


    let bar4 = SKSpriteNode(imageNamed:"Bar4")

    let bar5 = SKSpriteNode(imageNamed:"Bar5")

    let bar6 = SKSpriteNode(imageNamed:"Bar6")


    override func didMoveToView(view: SKView) {
        background.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidX(self.frame))
        addChild(background)
        background.yScale = 2

Upvotes: 1

Views: 1078

Answers (2)

zJorge
zJorge

Reputation: 798

I agree with vacawama's answer (I use the naming option) but I'd like to add a little on my approach to buttons in SK. Usually you either have a background and a label or just a PNG image with both background and label.

enter image description here

When using nodeAtPoint on the label you might not have enough area. If instead you use the background area to detect taps then you might have conflicts with the label on top of the node.

enter image description here

To solve this I create a third element (usually an SKShapeNode) on top of both, transparent and a little bigger to make it easier for the user to tap on it.

enter image description here

To make sure you are positioning these tap areas on top of the buttons remember to use a higher zPosition

enter image description here

Upvotes: 0

vacawama
vacawama

Reputation: 154533

Note that you are creating background and PlayButton nodes twice: once as a class property, and a second time as a local variable in didMoveToView. It is the local version that you are then adding to the scene as a child view. If you want to be able to test against these nodes in other methods in your class, get rid of the local declarations in didMoveToView. You can still fill in the other information about these nodes, just remove the let statements.

Then in your touchesBegan you want to get the location of the touch in the scene by passing self to touch.locationInNode(self) and then retrieve the node that corresponds to that touch:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        let node = self.nodeAtPoint(location)
        if node == Play_Button {
            println("Hello!!!!")
        }
    }
}

A second way to accomplish the PlayButton would be to give the node a name when you create it:

Play_Button.name = "playButton"

and then your touchesBegan would look like this:

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
    for touch: AnyObject in touches {
        let location = touch.locationInNode(self)
        let node = self.nodeAtPoint(location)
        if node.name == "playButton" {
            println("Hello!!!!")
        }
    }
}

Upvotes: 4

Related Questions