0

I have looked at all the other questions & answers but can't seem to find one that applies to me. I used waitForClick() and then the next line is making the ball appear. The ball shows but nothing else happens. Since it's in a while loop and the conditions are fulfilled, why isn't the program proceeding to the start of the while loop and continuing to execute?

I've seen answers that said to use move() in a loop, but that way I can't move the paddle or make the ball bounce.

1

2 Answers 2

2

Calling waitForClick() is going to cause your program to pause on that line of code until a click occurs. If waitForClick() happens to be inside of a while loop, then the loop will be paused.

You definitely do need to call move() inside of a loop. That is how you get your ball to keep moving. The example code provided already contains a while loop to get you started.

If waitForClick() isn't working for you, there are other functions you may want to consider inside the loop, such as waitForEvent(), and getNextEvent(). To summarise these briefly:

  • waitForEvent() will pause your program, in the same manner as waitForClick(). The difference is that it will return on the next event, whatever that is. This could be useful if you were to set up a timer before entering your loop, you can use waitForEvent() to respond to the timer event and update your game when the timer fires. This is one way to maintain a predictable frame-rate.
  • getNextEvent() will not pause your program, and your program will constantly be checking (or polling) for an event. If getNextEvent() used inside the while loop, the loop will run constantly.

Each function has trade-offs. Which function to use is ultimately up to you.

Refer to the online documentation has more information. The .h files inside the /spl directory also contain some examples which may be useful.

2
  • Thank you! I think I'll use getNextEvent. However, I won't have to use move afterwards, right?
    – Yao Li
    Aug 4, 2014 at 12:49
  • I implemented getNextEvent, but when I click, the game exits. Can you help me on this? Thanks!
    – Yao Li
    Aug 4, 2014 at 12:58
0

Well, I don't see the point behind waiting for a click then creating the ball. Besides, if you did as you described (i.e., called waitForClick() then created the ball), the ball won't show up until you click the mouse.

The move function is supposed to be called inside the while loop and that makes sense because the ball is constantly moving. The function move(), as its name suggests, moves the ball by a specific number of pixels on the x-axis and a specific number of pixels on the y-axis just one time. To keep it doing that, we need to put it into a loop.

But if we're gonna call move() inside the loop, how are we gonna move the paddle? Well, the paddle is supposed to be moving as your program receives a MOUSE_MOVED event. So you're supposed to be having a condition inside your loop that detects if this type of event occurs. If so, you move the paddle appropriately.

Notice that the body of the while loop executes really fast. So you won't be having issues with moving the paddle and the ball at the same time as you might have imagined.

What could possibly go wrong with your ball/paddle? The initBall() and initPaddle() functions both should return an instance of the ball and the paddle respectively (they shouldn't return NULL) after they create them and add them to the window so that you can access them using the variables named ball and paddle respectively in main() after they're assigned the values returned by these functions (which are the ball and the paddle GObjects that we created in these functions).

You must log in to answer this question.

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