0
votes

I've got 4 SKNode's in my Scene.

In TouchesBegan method, one of them has nil as name, even if I've assigned it on instantiation.

After touching another node, that node shows the correct name.

What could be the problem?

I'm a newbie with Sprite Kit Framework.

Here are initializations.

var rightPortBorder = SKSpriteNode(color: UIColor.yellowColor(), size: CGSize(width: 10, height: UIScreen.mainScreen().bounds.height/5))
rightPortBorder.position = CGPoint(x: UIScreen.mainScreen().bounds.width-5, y: (UIScreen.mainScreen().bounds.size.height/2))
rightPortBorder.name = rightPortCategoryName
var rightPortBody = SKPhysicsBody(rectangleOfSize: rightPortBorder.size)
rightPortBody.friction = 0.0
rightPortBody.dynamic = false
rightPortBody.contactTestBitMask = portCategory
rightPortBorder.physicsBody = rightPortBody
rightPort = rightPortBorder
self.addChild(rightPortBorder)

var leftPortBorder = SKSpriteNode(color: UIColor.yellowColor(), size: CGSize(width: 10, height: UIScreen.mainScreen().bounds.height/5))
leftPortBorder.position = CGPoint(x: 5, y: (UIScreen.mainScreen().bounds.size.height/2))
leftPortBorder.name = leftPortCategoryName
var leftPortBody = SKPhysicsBody(rectangleOfSize: leftPortBorder.size)
leftPortBody.friction = 0.0
leftPortBody.dynamic = false
leftPortBody.contactTestBitMask = portCategory
leftPortBorder.physicsBody = leftPortBody
leftPort = leftPortBorder
self.addChild(leftPortBorder)

var topPortBorder = SKSpriteNode(color: UIColor.redColor(), size: CGSize(width: UIScreen.mainScreen().bounds.height/5, height: 10))
topPortBorder.position = CGPoint(x: (UIScreen.mainScreen().bounds.size.width/2), y:  UIScreen.mainScreen().bounds.size.height-5)
topPortBorder.name = topPortCategoryName
var topPortBody = SKPhysicsBody(rectangleOfSize: topPortBorder.size)
topPortBody.friction = 0.0
topPortBody.dynamic = false
topPortBody.contactTestBitMask = portCategory
topPortBorder.physicsBody = topPortBody
topPort = topPortBorder
self.addChild(topPortBorder)

var bottomPortBorder = SKSpriteNode(color: UIColor.yellowColor(), size: CGSize(width: UIScreen.mainScreen().bounds.height/5, height: 10))
bottomPortBorder.position = CGPoint(x: (UIScreen.mainScreen().bounds.size.width/2), y: 5)
bottomPortBorder.name = bottomPortCategoryName
var bottomPortBody = SKPhysicsBody(rectangleOfSize: bottomPortBorder.size)
bottomPortBody.friction = 0.0
bottomPortBody.dynamic = false
bottomPortBody.contactTestBitMask = portCategory
bottomPortBorder.physicsBody = bottomPortBody
bottomPort = bottomPortBorder
self.addChild(bottomPortBorder)

And here's touches began method

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    var touch = touches.first as! UITouch
    var touchLocation = touch.locationInNode(self)

    if let body = physicsWorld.bodyAtPoint(touchLocation) {

         if body.node!.name == rightPortCategoryName {
            println("Began touch on right paddle")
            resetAllFlagsTouch()
            isFingerOnRightPort = true
        }
        else if body.node!.name == topPortCategoryName {
            println("Began touch on top paddle")
            resetAllFlagsTouch()
            isFingerOnTopPort = true
        }
        else if body.node!.name == bottomPortCategoryName {
            println("Began touch on bottom paddle")
            resetAllFlagsTouch()
            isFingerOnBottomPort = true
        }
        if body.node!.name == leftPortCategoryName {
            println("Began touch on left paddle")
            resetAllFlagsTouch()
            isFingerOnLeftPort = true
        }
    }
}
1
post your code please! show how you assign the name, and your touches began and everything else thats relevant - MaxKargin
I've added the code in the question!! - Bellots
Have tested your code and none of nodes print nil. What's in resetAllFlagsTouch()? - WangYudong
I reset all flags "isFingerOn...Port" - Bellots
And the problem isn't the node which is nil, but it's name. - Bellots

1 Answers

0
votes

I solved my problem editing the touchesBegan method: instead of getting the node from the world, I used the method locationInNode to get my node.

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
    /* Called when a touch begins */

    var touch = touches.first as! UITouch
    var touchLocation = touch.locationInNode(self)

    var node = self.nodeAtPoint(touchLocation)

        if node.name == rightPortCategoryName {
            println("Began touch on right paddle")
            resetAllFlagsTouch()
            isFingerOnRightPort = true
        }
        else if node.name == topPortCategoryName {
            println("Began touch on top paddle")
            resetAllFlagsTouch()
            isFingerOnTopPort = true
        }
        else if node.name == bottomPortCategoryName {
            println("Began touch on bottom paddle")
            resetAllFlagsTouch()
            isFingerOnBottomPort = true
        }
        if node.name == leftPortCategoryName {
            println("Began touch on left paddle")
            resetAllFlagsTouch()
            isFingerOnLeftPort = true
        }

}