When using detectCollision to get the ball to bounce off the paddle, I just need to check if detectCollison returns the object "paddle" and if so, reverse the movement. But for the brick, I have to first check if object == NULL....Why? If I just look for the ball to collide with a brick, via if(strcmp(getType(object), "GRect") ==0) , then making sure it's not the paddle rectangle, it gives me the "core dumped". Here's that section of code - the one that works.
If you're following the distribution code, you're likely to have a variable named
paddle that stores an instance of your paddle. If the ball collides with the paddle,
detectCollision returns an instance of the same paddle (the one stored in
paddle). You may think of the value that detect collision returns as an alias to the value stored in
paddle. At this point, you can easily check whether the returned
GObject is equal to
Although you could check whether the returned
GObject is equal to
NULL before comparing it with
paddle, this would be an unnecessary step since if the returned value is already
NULL, it won't be equal to
If you're like me, you never stored the
GRects that represent the bricks. If a ball collides with a brick,
detectCollision returns an instance of that brick (a
GRect) that is of course not the
paddle (which is another
GRect). But it may also return
NULL in which case I'll be risking a segmentation fault if I don't use that properly (e.g., passing this value to
getType) and that's why I need to check whether the returned value is equal to
NULL to make sure it's a brick before involving it in further operations.
If I had an instance of the brick, like I had one of the paddle, I could have compared the returned value with it directly and I wouldn't need to check whether the returned value is
Although I could have store the bricks, say in an array of
GRects, that would unnecessarily complicate things and I would have to compare the returned value with each of the bricks in the array until I find the matching instance of the brick because obviously I don't know directly which brick was returned.