Ios 斯威夫特:在点上检测身体。总是零
我正在努力发现为什么我不能用SpriteKit检测到Ios 斯威夫特:在点上检测身体。总是零,ios,sprite-kit,swift,Ios,Sprite Kit,Swift,我正在努力发现为什么我不能用SpriteKit检测到bodyAtPointbodyAtPoint始终返回nil,即使它出现时,我始终在点击精灵节点 代码如下: ... let spaceship = SKSpriteNode(color: UIColor.blueColor(), size: CGSizeMake(100, 100)) override func didMoveToView(view: SKView) { /* Setup your scene here */
bodyAtPoint
bodyAtPoint
始终返回nil
,即使它出现时,我始终在点击精灵节点
代码如下:
...
let spaceship = SKSpriteNode(color: UIColor.blueColor(), size: CGSizeMake(100, 100))
override func didMoveToView(view: SKView) {
/* Setup your scene here */
var borderBody = SKPhysicsBody(edgeLoopFromRect: self.frame)
self.physicsBody = borderBody
self.physicsBody.friction = 0.0
self.physicsWorld.gravity = CGVectorMake(0.0, 0.0)
spaceship.name = "spaceship"
spaceship.position = CGPointMake(400, 300)
var bodySize = CGSizeMake(spaceship.size.width / 1.15, spaceship.size.height / 1.15);
spaceship.physicsBody = SKPhysicsBody(rectangleOfSize: bodySize)
spaceship.physicsBody.dynamic = false
spaceship.physicsBody.restitution = 1.0
spaceship.physicsBody.friction = 0.0
spaceship.physicsBody.linearDamping = 0.0
spaceship.physicsBody.allowsRotation = false
self.addChild(spaceship)
}
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
/* Called when a touch begins */
super.touchesBegan(touches, withEvent: event)
var touch : UITouch! = touches.anyObject() as UITouch
var touchLocation : CGPoint! = touch.locationInNode(self)
if self.physicsWorld.bodyAtPoint(touchLocation) {
NSLog("true")
} else {
NSLog("false")
}
}
...
结果:
spaceship.physicsBody
输出:
<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode> name:'spaceship' texture:['nil'] position:{400, 300} size:{100, 100} rotation:0.00]
self.physicsWorld.bodyAtPoint(触摸位置)
始终是:
nil
。。。因此,条件始终返回false
谁能解释一下我哪里出错了?我希望最终检测到一个精灵节点上的触摸并执行一个动作
编辑:
即使我简化为以下内容,我仍然会得到false
:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
...
for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if self.physicsWorld.bodyAtPoint(location) {
NSLog("true")
} else {
NSLog("false")
}
}
}
...
非常感谢@defined的回答 诀窍是调用nodeAtPoint并访问nodeAtPoint.name
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location: CGPoint! = touch.locationInNode(self)
let nodeAtPoint = self.nodeAtPoint(location)
if nodeAtPoint.name {
println("NODE FOUND: \(nodeAtPoint.name)")
} else {
println("NULL")
}
}
}
如果要使用bodyAtPoint,则无法访问节点的名称。问题是,如果您想要像素完美的触摸检测,例如,您需要检测带有alpha部分的精灵的形状而不是矩形
诀窍是使用类别BitMask。您可以指定类别0x1当节点接近另一个具有附加粒子发射器的节点时,我遇到了类似的问题。似乎
nodeAtPoint()
检测到发射器,即使它当时是隐藏的
我通过使用可选的nodesAtPoint()
并迭代节点,直到找到我真正想要检测的节点,从而解决了这个问题
不确定z顺序粒子发射器是否将其带到堆栈顶部,但不想在这一阶段处理z顺序,在接触点查找所有接触的节点对我很有效。SpriteKit使用从左上角开始的原点还是需要将其还原到左下角?@很抱歉,但我不确定…还有一个方法叫做
self.physicsWorld.bodyInRect
,如果从SKNode子类化,请尝试调用self.physicsWorld.bodyInRect(self.frame)
。它是否返回一些值?您也可以使用:let location=touch.locationInNode(self)let nodeAtPoint=self.nodeAtPoint(location)println(“NODE AT POINT:\(nodeAtPoint)”)
@谢谢!我已经用答案更新了帖子。你需要使用nodeAtPoint.name代码>如果它是可选的。如果它不是可选的,那么如果nodeAtPoint.name
没有意义,因此需要更改。这并不能真正解释为什么bodyAtPoint不起作用,实际上正在做一些不同的事情。它查找其帧与该点相交的SKNode,该点与附加到SKNode的SKPhysicsBody相邻。SKNode累积帧不一定等于物理体路径,这意味着这两个“备选方案”并不总是产生相同的结果。我之所以提出这个问题,是因为我在swift中没有使用过这些方法,但在过去使用objective-c代码时使用过它们。
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
for touch: AnyObject in touches {
let location: CGPoint! = touch.locationInNode(self)
let nodeAtPoint = self.nodeAtPoint(location)
if nodeAtPoint.name {
println("NODE FOUND: \(nodeAtPoint.name)")
} else {
println("NULL")
}
}
}
let nodo = self.physicsWorld.bodyAtPoint(punto)
if (nodo?.categoryBitMask == 0x1 << 1) {
}