Joe Half Face
Joe Half Face

Reputation: 2333

How do I check if CGRect intersects with any SpriteNode?

A little of background: I have function spawnBubbles(), which uses output of another function determineSpawnPoint().

determineSpawnPoint() returns random CGPoint. There is also action, which spawns SpriteNodes once 0.5 second in the random X coordinate CGPoint.

The problem: as determineSpawnPoint() is random, sometimes 2 or 3 SpriteNodes in a row created nearby, so they intersect with each over.

What do I want to achieve: create a function

 func checkForFreeSpace(spawnPoint:CGPoint) -> Bool{
        //some code 
    }

which returns true if there is free space around certain point.

So, basically, when I get new random CGPoint, I want to implement a CGRect around it, and check if this rectangle intersects with some SpriteNodes (speaking in common sense, if there is free space around it)

Upvotes: 3

Views: 1770

Answers (1)

rakeshbs
rakeshbs

Reputation: 24572

You can create two CGRects from the point and nodes and use CGRectIntersectsRect function to check whether they intersect. The function returns true if they intersect.

if (CGRectIntersectsRect(rect1, rect2))
{
    println("They intersect")
}

Upvotes: 3

Related Questions