0

I can't make the ball move. I don't know where is the mistake. I hope you can help me.

Here is my code.

[code removed by mod to comply with academic honesty]

/**
 * Instantiates paddle in bottom-middle of window.
 */
GRect initPaddle(GWindow window)
{
    GRect paddle = newGRect (170, 550, 60, 5);
    setColor(paddle, "BLACK");
    setFilled(paddle, true);
    add(window, paddle);
    while (true)
    {
        // check for mouse event
        GEvent event = getNextEvent(MOUSE_EVENT);

        // if we heard one
        if (event != NULL)
        {
            // if the event was movement
            if (getEventType(event) == MOUSE_MOVED)
            {
                // ensure circle follows top cursor
                double x = getX(event) - 30;
                if (x<0)
                {
                    x=0;
                    setLocation(paddle, x, 550);
                }
                else if (x>340)
                {
                    x=340;
                    setLocation(paddle, x, 550);
                }
                else
                {
                    setLocation(paddle, x, 550);
                }
            }
        }
    }
    return NULL;
}

[code removed by mod to comply with academic honesty]

1
  • Also check your comments, as some of them were copy-pasted from other blocks of code and don't make sense in this context. – ChrisG Aug 6 '15 at 12:06
1

Your problem is with your initPadle() function. This function is supposed just to create the paddle and put it inside the window, not to move it too. Since you have the while (true) condition, and no breaks, this loop will never end, and thus the flow will never return to main to execute the other commands. That's why the ball doesn't move

0

You need to instantiate the ball and then call it from the main function.

You must log in to answer this question.

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