Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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)

like image 763
Joe Half Face Avatar asked Jan 25 '26 00:01

Joe Half Face


1 Answers

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")
}
like image 73
rakeshbs Avatar answered Jan 26 '26 17:01

rakeshbs