我的场景中有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)
这里的触摸开始了方法
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
}
}
}
答案 0 :(得分:0)
我解决了编辑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
}
}