Unit 3 Lesson 27 Project Debug ball not staying in play - disappears

Link: Pinball Machine Game

What he has created:
I have a student working on a pinball game for his final project. Instead of edgeSprites, he created sprites for his left, top, right, and diagonal walls for the pinball machine. He coded these walls to have the ball bounceOff of them and stay in play at all times unless the ball goes through the center of the flippers and into the bottom half of the game. Just like what would happen in a real pinball game. He did try edgeSprites first, but the ball did not react correctly at all in the game. The ball mostly disappeared… He also coded Flippers to have the ball bounce off of them as well. Please Note that right now, he has the right and left arrow buttons working the flippers (albeit to me opposite from what I think they should be). The up and down arrows will be eliminted. He originally had the left and rigt arrows rotating the flippers up and the up and down arrows resetting the flippers down. He figured out how to correctly code just the left and right flippers to move, but left the code in for the up and down arrows for now. I put a freeze on his coding until we got some answers on everything.

What is happening and what should happen:

  1. Sometimes, the flippers will shoot the ball out of play, even though the side walls are coded to have the ball bounceOff of them and keep the ball in the playing field. The ball goes out of play and disappears… This same thing happens with the very tip of the flippers - particularily the left flipper.
  2. Sometimes, the new ball put into play will get caught up along the top or side walls and it clings to the walls. The ball should never do this and should automatically be in play, bouncing off any walls it encounters. We have witnessed this with the top wall and the right side wall, as well as the top left corner. Any time this happens when the ball is y <70, the score will keep going up and up. We know the score is not the issue right now - as that will be fixed once the coding is correct to have those new balls bounce off of it… He did try adjusting the x and y of the new ball in play, but that still did not seem to fix the issue…
  3. It almost seems like there is a hidden square sprite in the middle of the game. Watching the reaction of the ball when it is hit with the flipper, often times, it will not go up in the center area of the game and it looks like the ball almost bounces off of something invisible in the center… The student used debug to see if any of his sprites needed a setCollider adjustment - but we can’t seem to find anything… I told him to keep the debug coding in the game while you all take a look at the code. The ball never seems to shoot to the stars from the flippers. Any thoughts on that? Are we missing something hidden?

Thank you all so much for any help you can give this student.

First, you are debugging too much at one time. Comment out most of that code and work on one thing at a time. Start with the ball falling like it is affected by gravity. Then make it bounce off the flipper. Then etc.

My initial assessment is that you have the bounciness up too high. I think this is because you are setting the velocityY of the ball to 2 every frame. That doesn’t simulate gravity. Instead, add some downward velocity to the ball each frame so instead of ball.velocityY = 2; make it ball.velocityY += 1; or maybe something smaller.

With the bounciness set so high, the ball gets moving real fast. So fast you may be stepping over the boundary sprites without touching them. Bring the speed way down, make the bumpers thicker, or also test for off screen.

You have some code

  if (ball.y > 400) {
    ball.x = 28;
    ball.y = 92;
  }

If the ball is moving fast enough you will put it into a fixed location every frame making it appear to be stationary.

Here is your code with some small changes to make the ball work but with most of the extra stuff commented out.

@sheri.mcnair,

What a fun game! Love the colors and sounds and overall design!

I concur with @jdonwells that when an object is moving at a high velocity, it can jump over the walls if they are not thick enough. What is happening is that if the walls are 10 pixels thick, but the velocity exceeds 10, the ball can be seen right before hitting the wall and then the next time through the draw loop, with a velocity of 20 (for example), it crosses the wall without touching it.

Once your student is able to fix that, it may help with some of the other issues or at least make them easier to address.

Please let us know if we can help further, though!

Mike

Thank you both so much! I have given these comments to the student and will be working with him this week to work this all out. I now have a clear picture of what is happening. That is all because you both took the time to take a look and edit the code. I appreciate the time you both took to help me out. It is so helpful!

1 Like