I have created an array of GReact to instantiate bricks

e.g. GReact brick[total_bricks];

now to identify whether the collision is with a brick, so I can delete the brick, I want to use something like:

if(object == brick[int])

but this doesn't work. Is there any solution to identify whether the colliding object is a brick.


Let's see if I can get you to think through to an answer without just spelling it out. ;-) Think about what kind of objects you need to detect collisions with. You could collide with the side of the window, but I'm sure you've dealt with that already. The ball itself is a circle, and it's the only circle. Besides, you don't need to worry about it colliding with itself. Other objects to worry about are the bricks, the paddle, and later, the scoreboard which is a label.

So now, ask yourself what collisions you need to handle? If you hit anything other than a brick or the paddle, do you need to do anything about it? (Remember, collisions with a wall, i.e., the edge of the window are handled by position, not by detecting collision.) Do the bricks and the paddle have any traits in common? And are they the only objects like this? And if so, can you tell the paddle from a brick? (It doesn't matter which brick.)

OK, once you figure that out, then you need to be able to tell a paddle from a brick. This should be easy for you to implement. Now, consider this. Once you hit a brick, you should be able to identify the brick by retrieving the object from the detect collision function and delete it. In fact, you probably don't even need to know which brick it is in the array.

I know this answer is a little cryptic, but I wanted to guide you to the answer, not hand it over on a silver platter. ;-)

  • Hello, Sir, thanks for the guidance but the thing I really wanted to know that if there is some kinda function, trick, or something using which we can tell that the object is a brick. Just like we can tell object is a label/paddle using: if (object == label)/(object == paddle) Something like, if (object == brick[int]) or if (object == brick[%i]) or something May 29 '15 at 9:03
  • 1
    there are several options available. You could check for the paddle with if (object == paddle), or you could determine an object's type using the getType(object) function. If it's a rectangle, it's either a brick or a paddle. If it's not a paddle, then you should know what to do next.
    – Cliff B
    May 29 '15 at 17:14
  • Yes, sir checking if it's not a paddle, in this case, would work. But if we had some other situation where there are an awful lot of other GRect and we have to filter out only the Brick. What to do in that case? We can't use too many OR conditions. Jun 3 '15 at 11:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .