Unit 3 Lesson 27 coding problem

My student is trying to have the bat hit the ball but it is not working properly.

@dcraig ,

It probably depends on exactly how that bounce is supposed to occur, but I think if this were my student, I would point out the documentation for the “bounce” function and show them how bounce affects both sprites (ie. both will bounce off of each other). Then, have them look at the other types of collision functions and perhaps choose one that better represents what they want to do.

Here’s the link to that documentation.

Hope this helps, but if not, please check back in!


1 Like

The bat does not fly off the screen now but we have encountered a problem that the ball is not moving when hit, it just bounces in position. What are we missing?

Hi @dcraig!

The error is in the if (bat.isTouching(baseball). The ball will only have the -y velocity as long as it is touching the bat so it is constantly changing from a positive velocity, then touching the bat, then negative velocity, then not touching the bat so back to positive velocity and over and over. An easy fix is to use the Boolean variable already in the game bat.debug which is set to true in the beginning. When the bat.isTouching(baseball), then change that variable to false. THEN if that bat.debug == false, you can move the ball in the negative y velocity and hit the ball. Here is a remix. I did remove the hit function.

Hope that helps!

Thank you. He got it to hit the ball correctly but now he is having difficulty making a loop so that it resets for the person to hit the ball again. What/how do I tell him to do this.

This is only my second year teaching this class and I am still having difficulty figuring these things out.

I appreciate all of your help!!!

Many things need to happen to reset. A good way to problem solve this is to use the Problem solving process and break it down into small chunks and figure out what the code is for each of those chunks.

What starts the whole process? What needs to be reset?
It starts with the Pitch() function so to be able to repeat this it should be controlled by the player vs automatically run when you start the game. So the Pitch() function needs to be adjusted so that it only occurs when “p” or any key is hit. That just means adding that keyDown block in the Pitch function. At least, that is how I would do it. So now “p” is clicked for pitch and “space” for hit.

What is the main conditions (IE if these occur) that would trigger the ball to reset to the pitcher?

  1. a homerun is hit aka in the homerun function - so the following should be in the homerun() function.

What needs to happen to reset the ball inside the homerun function?

  1. the ball would need to stop moving so 0 Y velocity
  2. the ball would need to go back to starting position so baseball.y=240;
  3. the bat.debug needs to reset back to true

What needs to happen to reset the bat inside the Pitch() function?

  1. if the keyWentUp(“space”)
  2. bat.rotation back to original rotation so it can swing again

Good luck!

1 Like

Thank you for your help.