0

Issue: the ball is not moving.

Code:

// keep playing until game over
while (lives > 0 && bricks > 0)
{
    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)
            {
                //move paddle
            }
        }
    }

    // BALL
    // initial velocity
    double velocity = 2.0;

    // bounce forever
    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_CLICKED)
            {

                // move circle along x-axis
                move(ball, velocity, 0);

                // linger before moving again
                pause(10);
            }
        }
    }

But, for some reason the ball is not moving. I have tried to put paddle and ball inside the same while (true) loop, but I get segmentation fault back.

3 Answers 3

1

The ball is never moving because you're having an infinite loop at the beginning of the outer while loop

while (lives > 0 && bricks > 0)
{
    while (true)
    {
        // do something
    }

    // CONTROL NEVER REACHES THIS LINE BECAUSE IT NEVER LEAVES THE PREVIOUS LOOP
}

After you fix this, you'll probably have another similar issue with the infinite loop at the end of the outer while loop. You probably don't need any loops inside of the outer loop.

Also, you need not to listen to a MOUSE_CLICKED in every iteration of the outer loop. This loop is basically responsible for the whole game. It's responsible for listening to MOUSE_MOVED events to move the paddle, for moving the paddle, for moving the ball, for bouncing the ball off, for detecting collisions, removing bricks, updating the score/lives and so on.

Although you might take some of these things out and place them in their own functions, you'll probably still call these functions from inside this loop.

Imagine that you're waiting for a click every time you wanna move the ball! That'd be a little boring, wouldn't it?

4
  • Thank You. The ball is moving and the paddle follows the mouse. The only problem I have now is the pause function.
    – julio
    Aug 27, 2014 at 1:25
  • @julio what's with it?
    – kzidane
    Aug 27, 2014 at 8:19
  • I'm sorry, I forgot to say that I fix it. Basically after the move function I added pause(10), by doing that I was pausing the paddle and the ball 10 milliseconds. So I removed the pause and decrease the speed of the ball.. now is working. THANK YOU!!
    – julio
    Aug 27, 2014 at 22:47
  • Right, I even use the 10 pause; but I still cannot repeat after twice played the game starting from the ball center in the window. Sep 1, 2015 at 3:08
1

You don't want the ball to move forever, only when lives>0 and bricks>0. If one of those two conditions are false, your ball should stop moving. Hence, no need to have the forever loop. You want the ball to move in the x-axis AND y-axis, so set a velocity in the x-direction and the in the y-direction. Use the bounce file as a preliminary step to get an understanding of the source code.

4
  • I do understand the ball moves only if lives>0 and bricks>0. Also, I know that the ball needs to move in a different direction. All I want to do as my first step to make the ball to move to any direction for now, then I will fix it.
    – julio
    Aug 25, 2014 at 3:25
  • You are declaring velocity inside a loop, which is why the ball may not be moving. Try implementing velocity before the while loop.
    – hunter
    Aug 25, 2014 at 3:29
  • I just moved out of the while loop, but is not working, the ball doesnt want to move :-(
    – julio
    Aug 25, 2014 at 3:34
  • I didn't understand what you said the first time I read it. Thanks for your help. Now the ball is moving. Thanks.
    – julio
    Aug 27, 2014 at 1:26
1

Are you sure the ball isn't moving? What happens if you lose 3 lives, do you call removeGwindow? If not, the ball may in fact have moved already, and you've lost 3 lives so your ball is frozen (unless you did call removeGwindow). Just for debugging purposes, try reducing velocity to something like .005 so you can see if it does move.

1
  • Thank You. By lower the speed I was able to fix a bug that I had. Thank You.
    – julio
    Aug 27, 2014 at 1:31

You must log in to answer this question.

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