SKNode calculateAccumulatedFrame() returns wrong frame

I'm writing a SpriteKit game in Swift that does this familiar dance in my GameScene touchesEnded(...)...

let touch = touches.first!.location(in: self)
let nodes = self.nodes(at: touch)

...but sometimes when the game is running, the nodes collection is empty, even though there are clearly, visibly nodes under the touch. All of the nodes I care about are children of a single SKNode, MapNode, that is one of two children of the GameScene.

After a bunch of debugging, I've noticed that when this problem happens, something is causing calculateAccumulatedFrame() to return a frame that is way, way smaller than the real bounds of MapNode.

When the scene begins, calculateAccumulatedFrame() is perfect, but something that happens later on breaks it. I've tried adding a node to MapNode that is the correct size to 'force' calculateAccumulatedFrame to do the right thing, but this doesn't work.

Is there something obvious I am doing wrong, or some common pitfalls? Is there some way to force calculateAccumulatedFrame to return this correct size?

Accepted Reply

I've solved the issue.

It turns out under some circumstances, the position of a node would become CGPoint(nan, nan), once this happens calculateAccumulatedFrame() returns the wrong bounds, which means some child nodes are not considered for self.nodes(at: touch) even though they are within the true frame of the parent node.

I've fixed the maths issue and now everything is fine.

Replies

I've solved the issue.

It turns out under some circumstances, the position of a node would become CGPoint(nan, nan), once this happens calculateAccumulatedFrame() returns the wrong bounds, which means some child nodes are not considered for self.nodes(at: touch) even though they are within the true frame of the parent node.

I've fixed the maths issue and now everything is fine.