Swift: detect bodyAtPoint. Always zero

I am trying to find out why I cannot detect bodyAtPointusing SpriteKit. bodyAtPointalways returns nil, even when it appears, I always click the sprite node.

Here is the code:

...

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

...

Results:

spaceship.physicsBody outputs:

<SKPhysicsBody> type:<Rectangle> representedObject:[<SKSpriteNode> name:'spaceship' texture:['nil'] position:{400, 300} size:{100, 100} rotation:0.00]

touchLocation conclusion:

(411.943664550781,553.014099121094)

self.physicsWorld.bodyAtPoint(touchLocation) is always:

nil

... Therefore, the condition always returns false.

Can someone explain where I'm wrong? I want to ultimately detect the touch of a node sprite and perform an action.

EDIT:

Even if I simplify the following, I still always get 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")
        }
    }
}

...
+4
source share
3 answers

bodyAtPoint, node. , , , , -.

categoryBitMask. 0x1 < 1, , , . - Uint32 , , , :

        let nodo = self.physicsWorld.bodyAtPoint(punto)
        if (nodo?.categoryBitMask == 0x1 << 1) {

        }

! , !

+1

@e .

, nodeAtPoint AtPoint.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")
        }

    }

}
0

, node, . , nodeAtPoint() , .

, nodesAtPoint() , node, .

, z, , z- , .

0

Source: https://habr.com/ru/post/1543378/


All Articles