0

I have used detectCollision to make the ball bounce off the paddle. I have tried to use it to make the ball bounce of the bricks but it does not. Actually, it does not compile. This is the piece of code I use

GObject object = detectCollision(window, ball)

if (object==paddle)

{

     velocityY=-velocityY;

}

if (object==brick)

{

     velocityY=-velocityY;

}

the error I get says: use of undeclared identifier 'brick' But I have brick returned from the function that returns the bricks. I guess it has something to do with the type of that function. I have tried to do it with

GObject object = detectCollision(window, ball)

if (strcmp(getType(object), "GRect") == 0)

{
     velocityY=-velocityY;
}

It compiles, but the ball does not move at all. Could please someone help me. I have been stuck for a week on that!

2
  • The thing that jumps out at me is that there are multiple bricks, so how can you compare object==brick? How are the bricks enumerated? Is it possible that when you create the bricks, only the last brick created is stored in the var brick? In order to figure out what's going on, we need to see how the bricks are created. As for the ball not moving with the second design, we'd need to see more of the code, particularly the code that handles movement. – Cliff B Apr 14 '15 at 4:55
  • If you implemented initBricks() like it is proposed in the specs, then the bricks are not returned to main by the function, and you can't use object == brick, you need to use the second approach, take a look at this question it is explained there: cs50.stackexchange.com/questions/2068/…. But you also need to check if the object is null, this other question would be helpful: cs50.stackexchange.com/questions/9294/… – wallek876 Apr 14 '15 at 7:37
1

You should use the second approach like other people suggested. In your code:

GObject object = detectCollision(window, ball)

if (strcmp(getType(object), "GRect") == 0)

{
     velocityY=-velocityY;
}

You didn't state that the object is the bricks. Find a way to state that and you'll be fine. Good luck.

3
  • Thank you for your help! – Dilyana Petrova Apr 15 '15 at 15:06
  • Do you have any suggestions how I can solve the other issiue-when I use the second approach: GObject object = detectCollision(window, ball) if (strcmp(getType(object), "GRect") == 0) { velocityY=-velocityY; } the ball does not move at all:( – Dilyana Petrova Apr 15 '15 at 15:07
  • @DilyanaPetrova, I believe you didn't put if (object != NULL) before detecting collision. – Viet Nguyen Apr 17 '15 at 7:03

You must log in to answer this question.

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