My student has a jet follow the arrow, which it does well. But two weird things happen as the jet approaches the mouse.
1 - It starts to stutter and then shakes violently in all directions after it hits the mouse.
2 - If the jet follows the mouse along a diagonal line, as it approaches the mouse the jet curves (hard to explain) at the last minute.
Any ideas re: what is going on?
Thank you very much.
What I expect to happen: [replace with a detailed description of the behavior you’re trying to create]
What actually happens: [replace with a detailed description of what actually happens when the code runs including any errors or unexpected behavior]
What I’ve tried: [replace with a detailed description of what you’ve already tried to do to solve the problem]
This is a tricky one … I set some watchers and I think what is happening is that the jet is constantly approaching the pointer, but at a velocity of 2, it passes it and then reorients and then quickly overshoots it again and then reorients, etc.
If you lower the velocity, the shaking is less, but still there.
I was able to remove the shaking by adding another conditional if you want to suggest something like this…
The first line checks to see if the jet is within three pixels of the mouse pointer and if so, it shuts down the velocity of the jet. Otherwise, it executes the code the student already wrote.
Thank you, Mike. That worked very well!
A couple of follow-up questions, if I may:
What does math.abs actually do?
My student wants to know if you can (a) adjust the speed of an item bouncing off another, or (b) have the bounce speed decelerate after the item bounces off something?
The student has added a soccer ball that the nose of the jet hits. He wants to change the speed of the bounce-off or have the all slow down as it moves farther from the jet. Does that make sense?
I was wondering if there was a way for the jet to “hit” the ball with a different force depending on how you pressed the direction key?
Thank you very much.
(Game Lab - Code.org)
math.abs is the code used to find the absolute value. In this case, I used it to find out how many pixels away the airplane sprite was from the pointer. This way whether the x value of the airplane was greater than the x value of the pointer or vice versa, it doesn’t matter. We just now how close it is.
When a collision occurs using bounceOff, the velocity of the ball is automatically increased, but you can use code to slow the velocity down. You may also want to consider which collision block to use. Here’s a post that talks about bounceOff in more depth.
I would suggest he play around with it and see what he can do on his own, but if he gets stuck with some code that isn’t working, check back in and we’ll try to help.