在TouchesBegan with Sprite Kit上的节点名称为nil



我的场景中有4个SKNode

TouchesBegan方法中,其中一个以nil作为名称,即使我在实例化时分配了它。

触摸另一个节点后,该节点显示正确的名称。

有什么问题吗?

我是使用Sprite Kit Framework的新手。

这些是初始化。

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)

这里是touchesbegan方法

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
        }
    }
}

我解决了编辑touchesBegan方法的问题:而不是从世界中获取节点,我使用locationInNode方法来获取我的节点。

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
        }
}

最新更新