0

Hi I have a bug for pset3 breakout problem, I have already implemented all the features but somehow when I am trying to hit the bricks i run to the core segmentation error, I will put the code inside while loop that we need to implement and see if anyone can detect the problem:

while (lives > 0 && bricks > 0) {

     // Check for mouse movement
    GEvent event = getNextEvent(MOUSE_EVENT);
    if (event != NULL)
    {
        // if the event was movement
        if (getEventType(event) == MOUSE_MOVED)
        {
            // paddle movement
            double x = getX(event) - getWidth(paddle) / 2;

            setLocation(paddle, x, HEIGHT -100);
        }
    }


  // bounce off top edge of window or right edge of window
   GObject object = detectCollision(window,ball);

    // checking for object detection collisions
    if (object == paddle)
    {
        // checking if the ball hits middle of the paddle
        if (getX(ball) == (getWidth(paddle) / 2))
        {
            // we want ball to go up vertically
            velocityX = 0;

        }
        // checking if the ball hits the left half of the paddle

        else if (getX(ball) > (getWidth(paddle) / 2))
        {
            // angling it towards right with some impulse from paddel
            velocityX = velocityX * 1.1;
        }
        else if (getX(ball) < (getWidth(paddle) / 2))
        {
            // angling it towards left with some impulse from paddle
            velocityX = -velocityX * 1.1;
        }

        // ball to go up
         velocityY = -velocityY ;
     }



    if (getX(ball) + getWidth(ball) >= getWidth(window))
    {

        velocityX = -velocityX;
    }

        // bounce off left edge of window or bottom edge of window
    if (getX(ball) <= 0)
    {
        velocityX = -velocityX;

    }

  // checking to see if ball goes above the screen
    if (getY(ball) + getHeight(ball) <= 0) 
    {
     // change the y direction
        velocityY = -velocityY;
    }

 ;
    if (strcmp(getType(object),"GRect") == 0)
    {
        // removing the brick
        removeGWindow(window,(getType(object),"GRect")); 
        // changing direction
        velocityY = -velocityY;
        // updating score
        points++;

    }

       move(ball,velocityX , velocityY );
     // linger before moving again
    pause(10)

--- Everything works well up to the if (strcmp(getType(object),"GRect") == 0), and that's where I run in to the core segmentation problem. Any help would appreciated Thank you.

1 Answer 1

0

It looks to me like your problem doesn't occur when a collision is detected, but when one is not detected. If there is no collision, then null will be returned from detectCollision() and assigned to object. Later, the code tries to execute the statement you identified:

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

When object == null, object->type doesn't exist. When it then tries to do a strcmp() using a nonexistent object, it generates a seg fault.

It should be obvious what you need to add to your code, so good luck. ;-)

If this answers your question, please click on the check mark to accept. Let's keep up on forum maintenance. ;-)

You must log in to answer this question.

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