Search code examples
swiftsprite-kitconstraintssklabelnode

Can not select node under touchBegan


I have a game and in the playScene I have made a function to creat a SKLabeleNode. Now I can call this function to add the node to the scene, it works fine and it will add the node to the scene, so the next thing is I now need to be able to select the node I just added to the scene. ///////////////////// THE PROBLEM IS AT THIS POINT ////////////////////////

Here I will select a node I created earlier and compare it to the SKLabelNode created in the function and if the node created in the function is == to the node I first selected I need to remove the node created by the function. THE PROBLEM IS I can not select the node that was created in the function

//THIS IS THE SMALLEST VERSION I CAN MAKE THAT CAN BE COPY AND PASTED INTO A GameScene Class AND WORK. //THE ONLY THING THAT HAS TO BE DONE IS MAKE THE SKSpriteNode(imageNamed: "A") AND SKSpriteNode(imageNamed: "B")

import SpriteKit import Foundation class GameScene: SKScene {

var yellowColor = UIColor.yellowColor()
var redColor    = UIColor.redColor()

let upperCaseA  = SKSpriteNode(imageNamed: "A")
let upperCaseB  = SKSpriteNode(imageNamed: "B")

var smallSizeLatter  = CGSize()
var largeSizeLatter  = CGSize()

var tempVarToHoldPlaceToMakeItRun = SKLabelNode(text: "A")

var latterADefaults:   NSUserDefaults = NSUserDefaults.standardUserDefaults()
var latterBDefaults:   NSUserDefaults = NSUserDefaults.standardUserDefaults()

override func didMoveToView(view: (SKView!)) {

    _ = NSTimer.scheduledTimerWithTimeInterval(1.0, target: self, selector: ("fallTheText"), userInfo: nil, repeats: true)

    self.backgroundColor = UIColor.whiteColor()

    latterADefaults.setBool(true, forKey: "latterADefaultsKey")
    latterBDefaults.setBool(true, forKey: "latterBDefaultsKey")
    smallSizeLatter      = CGSize(width: 37, height: 37)
    largeSizeLatter      = CGSize(width: 54, height: 54)
    upperCaseA.size      = smallSizeLatter
    upperCaseA.position  = CGPoint(x: self.frame.width / 8, y: self.frame.height  * 0.75)
    upperCaseB.size      = smallSizeLatter
    upperCaseB.position  = CGPoint(x: self.frame.width / 8, y: self.frame.height  * 0.65)

    self.addChild(upperCaseA)
    self.addChild(upperCaseB)

}
func fallTheText(){

    let theFallingText      = SKLabelNode(text: "A")

    let theLowerCaseLatter: [String] = ["a", "b",]
    let textIndex               = Int(arc4random_uniform(UInt32(2)))
    let lowerCaseLatter               = theLowerCaseLatter[textIndex]
    theFallingText.text     = lowerCaseLatter

    theFallingText.name = lowerCaseLatter

    print("the name is", theFallingText)


    let colorListOfColors: [UIColor] = [yellowColor, redColor,]
    let colorIndex                   = Int(arc4random_uniform(UInt32(2)))
    let colorOfColors                = colorListOfColors[colorIndex]
    theFallingText.fontColor     = colorOfColors


    theFallingText.fontName  = "HoeflerText-BlackItalic"
    theFallingText.fontSize  = 40


    func randomRange (lower: UInt32 , upper: UInt32) -> UInt32 {
        return lower + arc4random_uniform(upper - lower + 1)
    }
    let minValue                 = self.size.width / 3
    let maxValue                 = self.size.width - 50
    let uppers                   = UInt32(maxValue)
    let lowers                   = UInt32(minValue)
    let spawnPoint               = CGFloat(randomRange(lowers, upper: uppers))
    theFallingText.position  = CGPoint(x: spawnPoint, y: self.size.height * 0.85)


    let actionY                  = SKAction.moveToY(-30, duration: 20)
    theFallingText.runAction(SKAction.repeatActionForever(actionY))
    self.addChild(theFallingText)
}

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
    for touch: AnyObject in touches{
        let positionOfTouch = touch.locationInNode(self)
        let nodeAtLocation = self.nodeAtPoint(positionOfTouch)
        if nodeAtLocation == upperCaseA {
            if latterADefaults.boolForKey("latterADefaultsKey"){
                upperCaseA.size = largeSizeLatter
                latterADefaults.setBool(false, forKey: "latterADefaultsKey")
                upperCaseB.size = smallSizeLatter
                latterBDefaults.setBool(true, forKey: "latterBDefaultsKey")
            }else{
                upperCaseA.size = smallSizeLatter
                upperCaseB.size = smallSizeLatter
                latterADefaults.setBool(true, forKey: "latterADefaultsKey")
            }
        }

////////THE PROBLEM IS HERE WHERE I ADDED THIS TEMP NODE TO MAKE IT RUN/////////

        if nodeAtLocation == tempVarToHoldPlaceToMakeItRun{
            if tempVarToHoldPlaceToMakeItRun.text == "yellow"{
                print("danny", tempVarToHoldPlaceToMakeItRun.name)
                if upperCaseA.size == largeSizeLatter{
                    tempVarToHoldPlaceToMakeItRun.removeFromParent()
                    upperCaseA.size = smallSizeLatter
                }


            }
        }
        if nodeAtLocation == upperCaseB {
            if latterBDefaults.boolForKey("latterBDefaultsKey"){
                upperCaseA.size = smallSizeLatter
                latterADefaults.setBool(true, forKey: "latterADefaultsKey")
                upperCaseB.size = largeSizeLatter
                latterBDefaults.setBool(false, forKey: "latterBDefaultsKey")
            }else{
                upperCaseB.size = smallSizeLatter
                latterBDefaults.setBool(true, forKey: "latterBDefaultsKey")
            }
        }
    }
}

}

How can I touch this node to compare it? I have changed the function to return a SKLabelNode with no luck. I tried to use the function with no return value no luck. I have removed the NSTimer and added the function as a self.addChild(fallTheText) with no luck. The SKLabelNode created in the function is flowing down the scene in all cases. OK now when I try to compare them, I touch the first node and it reacts but it also now adds a new node from the function, and it should not. When I try to select the falling node created by the function it just keeps adding more nodes from the function and will not select the node that is falling.

The objective is have random falling latter and a fixed image 'that will resize when touched' compare them and then remove the fallen tent letter.


Solution

  • Having a node in a function and adding the function as a child to the scene will scroll the text down the scene but it does not add the node to the parent.

    I moved the node outside the function and created a new function to handle the moving of the node.

    func timer1(){
        theFallingText.removeActionForKey("actionYKey")
        let actionYY = SKAction.moveToY(self.size.height * 0.85, duration: 0.0)
        theFallingText.runAction(SKAction.repeatAction(actionYY, count: 1))
        fallTheText()    
    }
    

    I had to call the fallTheText function in the didMoveToView.