MadShark
MadShark

Reputation: 139

intersectsNode not detecting intersection

I'm building an app using spriteKit (objective-C), and I'm using intersectsNode to tell if a player-drawn line collides with letter tiles on the gameboard. I have another instance of intersectsNode in my program that is working fine, but for some reason this new scenario always returns false. I've checked the parameters, and the only thing spriteKit says is make sure they are both children of the same tree, which they definitely are (self). One is a SKSpriteNode, and the other is a SKShapeNode.

if (directionIsDecided == TRUE) {
    for (letterTile in letterTileArray) {
        if ([letterTile intersectsNode:selectorLine]) {
            [lettersToBeCalculatedForPoints addObject:letterTile];
        }
    }
}

Hopefully I'm not overlooking something stupid... Any ideas what could cause this function to never return true?

*edit

Ok, I think it has something to do with how I'm drawing my line. I changed it up so it detects collision with the vowels that have been spawned on the board, each vowel being a child of letterTile, and now it tells me that ALL vowels are colliding with the selecorLine, even if it doesn't touch any of them.

This is how I created my selectorLine:

Implementation:

    CGMutablePathRef pathToDraw;
    SKShapeNode *selectorLine;

TouchesBegan:

   UITouch* touch = [touches anyObject];
   CGPoint positionInScene = [touch locationInNode:self];

   pathToDraw = CGPathCreateMutable();
   CGPathMoveToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);

   selectorLine = [SKShapeNode node];
   selectorLine.path = pathToDraw;
   selectorLine.strokeColor = [SKColor greenColor];
   selectorLine.lineWidth = 10;
   [self addChild:selectorLine];
   selectorLine.zPosition = 101;

TouchesMoved:

    UITouch* touch = [touches anyObject];
    CGPoint positionInScene = [touch locationInNode:self];
    CGPathAddLineToPoint(pathToDraw, NULL, positionInScene.x, positionInScene.y);
    selectorLine.path = pathToDraw;

Any and all ideas are appreciated, thanks!

Upvotes: 3

Views: 3114

Answers (4)

Ali Beadle
Ali Beadle

Reputation: 4516

Similar problem, in my case one of the nodes I was comparing was simple SKNode grouping together other node types. An SKNode has a position but no width nor height so it does not 'intersect' anything.

For me a combination of @JoePasq and @Theis Egeberg's answers was needed:

if node.calculateAccumulatedFrame().intersects(sprite.frame) {...

(note if both nodes are simple SKNodes then use caluclateAccuulatedFrame in both positions, in my case sprite was an SKSpriteNode so a simple .frame is fine.)

Upvotes: 1

fearmint
fearmint

Reputation: 5334

I have been working around this in my code by using CGRectIntersectsRect for SKShapeNode bounding rectangle intersections.

Using CGRectIntersectsRect your code would look like:

if (directionIsDecided == TRUE) {
    for (letterTile in letterTileArray) {
        if (CGRectIntersectsRect(letterTile.frame, selectorLine.frame)) {
            [lettersToBeCalculatedForPoints addObject:letterTile];
        }
    }
}

Upvotes: 5

Theis Egeberg
Theis Egeberg

Reputation: 2576

intersectsNode uses the calculateAccumulatedFrame method.

It's stated a few places deep deep in the SpriteKit documentation that you should override this method with your own in many cases. After reading that I ended up doing it all the time. SKShapeNodes are a prime example of this. But if you subclass a SKNode and fill it with other content you'll probably also not want all of it to "intersect" as per usual.

So override the calculateAccumulatedFrame method for your shapenode subclass, and it should intersect nicely again.

Upvotes: 4

Dvole
Dvole

Reputation: 5795

I had the same problem as you. I had an array undeclared so adding objects to it did nothing and there were no nodes to check against. Double check that your letterTileArray is not empty.

Upvotes: 1

Related Questions