1

I keep on getting a segmentation fault on the function below and have no idea why. I used GDB to find where the fault was occurring but couldn't figure out any more than that.

// keep playing until game over
while (lives > 0 && bricks > 0)
{
    if (getEventType(event) == MOUSE_MOVED)
    {
        // ensure paddle follows cursor horizontally
        double x = getX(event) - getWidth(paddle) / 2;
        setLocation(paddle, x, HEIGHT-PHEIGHT-25);

        // stop paddle at right boundary
        if (getX(paddle) + getWidth(paddle) >= getWidth(window))
        {
            setLocation(paddle, WIDTH-getWidth(paddle), HEIGHT-PHEIGHT-25);
        }

        // stop paddle at left edge of window
        else if (getX(paddle) <= 0)
        {
            setLocation(paddle, 0, HEIGHT-PHEIGHT-25);
        }
    }

The segmentation fault occurs on the double x line (found using debugger).

0

I think that you are forgetting to get a new event when you re-iterate through the while loop. such that the corrected code would look like this:

// keep playing until game over
while (lives > 0 && bricks > 0)
{
    // check for mouse event
    GEvent event = getNextEvent(MOUSE_EVENT);

    // if we heard one
    if (event != NULL)
    {

        if (getEventType(event) == MOUSE_MOVED)
        {
            // ensure paddle follows cursor horizontally
            double x = getX(event) - getWidth(paddle) / 2;
            etc...

You must log in to answer this question.

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