Technology

9.7.4 Leash CodeHS Answers: The Code That Keeps the Ball on Its Leash

A ball sits in the middle of the canvas. You move the mouse to the right and the ball follows, pulling a line behind it. Move back across the screen and the line swings the other way. For the effect to work, one end of the line has to stay exactly where it started, even when the ball changes direction.

That fixed point is the detail to watch when you’re working through 9.7.4 Leash CodeHS Answers. The program needs to remember where the canvas centre is and read where the pointer is now. The code below shows how to use those two positions, why the line can seem to disappear, and how to work out what went wrong by watching the ball move.

What the CodeHS Leash Exercise Asks You to Make

The Leash exercise in the CodeHS Drawing Lines lesson begins with a circle and a line. The circle starts in the middle of the canvas. Both ends of the line start there too, so there’s no distance between them. That’s why the first thing you see is a ball sitting on its own.

Once you move the mouse, the program puts the centre of the ball at the pointer. The line’s far endpoint goes to the same spot. Its starting point stays in the middle. If you move straight to the right, the line stretches horizontally. If you move below the centre, it tilts downwards.

Take a canvas that measures 400 pixels across and 500 pixels down. The centre is (200, 250). Move the pointer to (320, 120) and the line runs from (200, 250) to (320, 120). Move again to (80, 380) and only the second pair changes. Those canvas coordinates are enough to describe the whole effect.

9.7.4 Leash CodeHS Answers With the Complete JavaScript Solution

This example uses a 30-pixel ball radius. If your starter code already defines BALL_RADIUS, use the existing declaration and keep a single copy of it in your program.

var BALL_RADIUS = 30;
var ball;
var leashLine;

function start() {
    var anchorX = getWidth() / 2;
    var anchorY = getHeight() / 2;

    leashLine = new Line(anchorX, anchorY, anchorX, anchorY);
    add(leashLine);

    ball = new Circle(BALL_RADIUS);
    ball.setPosition(anchorX, anchorY);
    add(ball);

    mouseMoveMethod(moveBallAndLine);
}

function moveBallAndLine(event) {
    var mouseX = event.getX();
    var mouseY = event.getY();

    ball.setPosition(mouseX, mouseY);
    leashLine.setEndpoint(mouseX, mouseY);
}

The setup begins by dividing the width and height of the canvas by two. It uses the results as the coordinates for the fixed anchor. The Line constructor takes four values. The first pair gives the starting point, and the second gives the endpoint. Both pairs use the anchor coordinates at the start.

Next, the program adds the line and then places the ball over it. This drawing order lets the ball cover the last part of the line. On screen, the leash appears to meet the edge of the ball, even though the endpoint sits at the ball’s centre.

The mouseMoveMethod() call registers moveBallAndLine as the event handler. Each time the mouse moves across the canvas, the handler gets the pointer’s current x and y values. Then it uses the same pair in ball.setPosition() and leashLine.setEndpoint(). The two objects stay connected because both updates use the same position from the same event.

How to Fix a Leash That Comes Loose

When you’re checking 9.7.4 Leash CodeHS Answers against your own work, pay attention to what moves first. If the ball follows the pointer while the line stays behind, the handler may be updating only the circle. Check that the next line calls leashLine.setEndpoint(mouseX, mouseY).

If the anchor slides across the canvas, you may have used leashLine.setPosition(). That method changes the line’s starting point. The same method name does a different job on a circle, where it sets the centre. Use setPosition() for the ball and setEndpoint() for the moving end of the line.

If the ball stays in the middle even when you move the pointer, check how you registered the handler. mouseMoveMethod(moveBallAndLine) passes the function to CodeHS. Adding parentheses after moveBallAndLine would call it during setup, before it receives the mouse event.

An error about an undefined ball can also come from where the variable is declared. The example declares ball above start() so the handler can use it. If you declare another var ball inside start(), the circle belongs to that local variable and the shared ball never receives it.

A Quick Canvas Test That Reveals Most Mistakes

With the program running, move the pointer from the middle towards the right edge. Pause partway across. The ball should stop with the pointer, and you should see where the line begins. Now sweep to the left. Watch the centre of the canvas while the line turns around it.

For the last check, bring the pointer back towards the starting point. The line should shorten as the ball approaches the anchor. Once the distance between them is smaller than the ball’s radius, the ball can cover the whole line. That can make it seem to disappear just before the pointer reaches the exact centre.

If you see extra circles or a trail of lines, check for new Circle(), new Line(), or add() inside the mouse handler. In the example, start() creates the shapes once. Each mouse movement updates the ball and line that are already on the canvas.

Conclusion

The idea behind 9.7.4 Leash CodeHS Answers comes down to one fixed point and one moving point. Set the anchor during setup, then give the ball and the far end of the line the same coordinates whenever the mouse moves. Sweep the pointer across the canvas one last time. If the ball follows and the line pivots around the centre, you’ve made the two objects move together exactly as the exercise asks.

(FAQs)

Why does the ball start at the centre of the canvas?

The setup calculates half the canvas width and half its height, then passes those values to ball.setPosition(). That places the centre of the circle at the middle of the drawing area.

Do I have to click or drag the mouse?

Moving the mouse is enough. mouseMoveMethod() runs the handler when the pointer moves across the canvas. If the ball only moves while you hold down the mouse button, check whether you used mouseDragMethod().

Why does the line look shorter than the distance to the pointer?

The endpoint sits at the centre of the ball, but the ball covers that last stretch of the line. You see the leash from the anchor to the ball’s edge.

Can the ball go partly outside the canvas?

Yes. The pointer controls the ball’s centre. Near an edge, some of the circle can extend past the drawing area. The whole circle comes back into view when you move the pointer inwards.

Does the Leash program need a loop or a timer?

The mouse movement event tells the program when to update. Every time CodeHS receives that event, the handler reads the new position and moves the ball and the line endpoint together.

nuvoramagazine.co.uk

Related Articles

Back to top button