gpt4 book ai didi

swift - TouchesBegan 上使用 Sprite Kit 的 SkNode 名称为 nil

转载 作者:行者123 更新时间:2023-11-30 14:09:13 25 4
gpt4 key购买 nike

我的场景中有 4 个 SKNode

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

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

可能是什么问题?

我是 Sprite Kit 框架的新手。

这里是初始化。

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

最佳答案

我解决了编辑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
}

}

关于swift - TouchesBegan 上使用 Sprite Kit 的 SkNode 名称为 nil,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31946468/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com