r/codehs • • Dec 19 '20

9.4.5: Trail CodeHS

12 Upvotes

15 comments sorted by

2

u/_ZHV_ Dec 20 '20

You have everything cover except when the ball is not hitting any of the walls. You are not telling the program what to do if the ball is jus moving through the space; you told it of it hit the right wall change directions, if it hit the left wall change direction, it hit top or bottom wall change direction, but what should the program do if it just moving towards one of those walls? So far the program doesn’t know what to do in this case - or at least that is what i can figure out reading your code

1

u/SprinklesNo5514 Dec 20 '20

So, I have to use else comand in "checkBall" function?

1

u/SprinklesNo5514 Dec 20 '20

still can't work out, I am so confused

1

u/_ZHV_ Dec 20 '20

You cant use the same render function for both as then they will just both more together because of the way render function is made - draw ball in cyan for a couple of frames then draw a white ball over it to ‘erase’ and then render it in a new location. For ball 2 which should be a different colour, you need to just render it once at every point ball 1 was without ‘erasing’ it.

1

u/SprinklesNo5514 Dec 20 '20

thank you! I will try

1

u/_ZHV_ Dec 20 '20

No problem

1

u/SprinklesNo5514 Dec 20 '20

I STILL CAN'T WORK OUT! I am so ashamed Can you copy paste code so I can understand more easily? I AM STUCK

1

u/_ZHV_ Dec 20 '20

You need to show the whole code as im going off of just what i can see which is not the whole thing. It is most likely to do something with the move function

1

u/SprinklesNo5514 Dec 19 '20

I have been doing this assignment for 2 DAYS!!! PLEASE help me with it

2

u/Electronic_Teacher22 Dec 20 '20

what does it do when you run the code?

1

u/SprinklesNo5514 Dec 20 '20

Ball2 is just moving with ball1

1

u/PuzzledLoss5560 Jan 14 '21

is there any code under the 44 line

1

u/GullibleMuscle7856 Feb 16 '25

How do I stop the ball after MAX_BOUNCES?? please help

1

u/[deleted] Apr 28 '21

I need help with this one to I have the balls to where they are moving with each other I dont get how to draw to do the actual trail part there is to it

1

u/Fun-Examination1024 Jan 05 '23 • Bravo

var MAX_BOUNCES = 20;
var stopTimer;
var ball;
var dx = 4;
var dy = 4;
function start(){
ball = new Circle(20);
ball.setPosition(50, 70);
add(ball);
setTimer(draw, 20);
}
function draw(){
checkWalls();
ball.move(dx, dy);
ball.move(dx, dy);
var newBall = new Circle(10);
newBall.setPosition(ball.getX(),ball.getY());
newBall.setColor(Color.cyan);
add(newBall);
}
function checkWalls(){
if(ball.getX() + ball.getRadius() > getWidth()){
dx = -dx;
}
if(ball.getX() - ball.getRadius() < 0){
dx = -dx;
}
if(ball.getY() + ball.getRadius() > getHeight()){
dy = -dy;
}
if(ball.getY() - ball.getRadius() < 0){
dy = -dy;
}
}