Looking for 9.7.4 Leash CodeHS answers? This exercise teaches you how to use JavaScript to create a ball and a line on the screen. The ball follows your mouse, while the line stays connected to it.
In this guide, you will find a sample code, simple steps to understand it, and tips to fix common errors. You will also learn how mouse events work in CodeHS.
Note: CodeHS exercise numbers can change between courses. Check your lesson instructions to make sure this guide matches your assignment.
Table of Contents
ToggleWhat Is 9.7.4 Leash in CodeHS?
The Leash exercise is a JavaScript activity that teaches you how to make moving shapes on a screen.
It uses two main objects:
- Ball: A circle that follows your mouse.
- Leash: A line that connects the ball to a fixed point.
When you move your mouse, the ball moves with it. The line also changes to stay connected to the ball.
This exercise helps you learn how to use shapes, mouse events, and screen positions in JavaScript.
What Should the Program Do?
Your program should:
- Show a ball on the screen.
- Draw a line from the center of the screen to the ball.
- Move the ball when the mouse moves.
- Change the end of the line to follow the ball.
- Keep the ball and line connected.
Check Your CodeHS Course Version
CodeHS has used different exercise numbers in different courses.
For example, the older Video Game Design course lists a Leash exercise as 4.7.4. Another course version includes a Leash challenge as 27.2.12.
Check your lesson title and instructions before using any code.
You can visit the official CodeHS course outline for more information.
9.7.4 Leash CodeHS Answers: JavaScript Code
Here is a sample solution that shows how to create the ball and line and make them follow mouse movement.
var BALL_RADIUS = 30;
var ball;
var line;
function start() {
ball = new Circle(BALL_RADIUS);
ball.setColor(Color.yellow);
var centerX = getWidth() / 2;
var centerY = getHeight() / 2;
line = new Line(centerX, centerY, centerX, centerY);
add(line);
add(ball);
ball.setPosition(centerX, centerY);
mouseMoveMethod(moveBall);
}
function moveBall(e) {
var x = e.getX();
var y = e.getY();
ball.setPosition(x, y);
line.setEndpoint(x, y);
}
Note: This is a sample code for the behavior explained in this guide. Your CodeHS assignment may have extra rules, so check your lesson before submitting.
What Does This Code Do?
When the program starts:
- A yellow ball appears in the center.
- A line is created at the same point.
- The program waits for mouse movement.
When you move your mouse, the ball follows it. The line also changes its end point to stay connected to the ball.
Understanding the Leash Code Step by Step
1. Creating Variables
var BALL_RADIUS = 30;
var ball;
var line;
The first line sets the size of the ball.
The other two variables store the ball and line. We create them outside the functions so that different functions can use them.
2. Finding the Center of the Screen
var centerX = getWidth() / 2;
var centerY = getHeight() / 2;
getWidth() gives the screen width.
getHeight() gives the screen height.
Dividing these numbers by two finds the center of the screen.
For example, if the screen is 400 pixels wide and 400 pixels high, its center is at X = 200 and Y = 200.
3. Creating the Line
line = new Line(centerX, centerY, centerX, centerY);
This code creates the leash.
A line uses four values:
new Line(x1, y1, x2, y2);
The first two values show where the line starts. The last two values show where it ends.
Both points start at the center of the screen.
4. Creating the Ball
ball = new Circle(BALL_RADIUS);
ball.setColor(Color.yellow);
ball.setPosition(centerX, centerY);
The first line creates a circle.
The second line makes it yellow.
The third line places it in the center of the screen.
We also use add() to show the ball and line:
add(line);
add(ball);
5. Adding the Mouse Event
mouseMoveMethod(moveBall);
This tells the program to run the moveBall() function when the mouse moves.
The function name does not need brackets here because we are telling JavaScript which function to use.
6. Moving the Ball and Line
var x = e.getX();
var y = e.getY();
These lines get the mouse position.
getX()gets the left-to-right position.getY()gets the up-and-down position.
Next, we move the ball:
ball.setPosition(x, y);
Then, we move the end of the line:
line.setEndpoint(x, y);
Both use the same mouse position. This keeps the line connected to the ball.
Mouse Events in CodeHS
Mouse events let your program respond when someone uses a mouse.
CodeHS has two useful mouse functions.
What Is mouseMoveMethod()?
mouseMoveMethod(moveBall);
This function runs when the mouse moves over the screen.
It is useful when you want an object to follow the mouse without holding down a button.
What Is mouseDragMethod()?
mouseDragMethod(moveBall);
This function runs when someone moves the mouse while holding down a mouse button.
It is useful when an exercise asks you to drag an object.
What Is the Difference?
| Mouse function | What it does |
|---|---|
mouseMoveMethod() | Works when the mouse moves |
mouseDragMethod() | Works when the mouse moves while a button is held |
Choose the function that matches your assignment instructions.
Common 9.7.4 Leash CodeHS Errors and Fixes
Sometimes your code may not work as expected. Here are some common problems and simple ways to fix them.
Why Does the Leash Line Disappear?
Check these things:
- Make sure you added the line using
add(line). - Check that the line has the correct starting and ending points.
- Make sure the
linevariable is not changed by mistake. - Check that the line is inside the visible screen area.
Why Does the Ball Not Follow the Mouse?
Check that you wrote the mouse event correctly:
mouseMoveMethod(moveBall);
Also, check that you used the correct mouse positions:
var x = e.getX();
var y = e.getY();
Make sure the ball is created before the mouse function tries to move it.
Why Does the Whole Line Move?
There is a difference between moving a full line and changing only its end point.
line.move(10, 10);
This moves the whole line.
line.setEndpoint(x, y);
This changes only the end of the line.
For the leash effect, changing the endpoint is usually the correct choice.
Why Do Multiple Balls Appear?
This can happen if you create a new ball every time the mouse moves.
Create the ball once inside start():
ball = new Circle(BALL_RADIUS);
Then move the same ball with:
ball.setPosition(x, y);
This stops the program from making extra balls.
How Do You Fix Undefined Variable Errors?
An undefined variable error means the program cannot find a variable.
To fix it:
- Check that
ballandlineare declared. - Make sure they are created before they are used.
- Check the spelling of each variable.
- Make sure the event function can access them.
- Remember that JavaScript uses capital and small letters differently.
For example, ball and Ball are not the same variable.
How to Test Your CodeHS Leash Program
Before submitting your work, run the program and check these things.
Check the Starting Position
Make sure the ball and line appear in the center of the screen.
Move Your Mouse
Move your mouse in different directions:
- Left and right.
- Up and down.
- Diagonally.
- Near the screen edges.
The ball should follow the mouse, and the line should stay connected.
Final Testing Checklist
- The ball appears on the screen.
- The line is visible.
- The ball follows the mouse.
- The line changes its endpoint.
- The starting point stays in place.
- No extra balls appear.
- The program has no errors.
If your code works but CodeHS does not accept it, check the assignment instructions again. Your exercise may have extra rules.
JavaScript Skills You Learn from the Leash Exercise
This exercise teaches some useful JavaScript skills.
Global Variables
Global variables can be used by different functions. In this exercise, both start() and moveBall() use the same ball and line.
Functions and Mouse Events
The start() function sets up the program. The mouse function runs when someone moves the mouse.
Screen Positions
The X value controls left and right movement. The Y value controls up and down movement.
Working with Shapes
You learn how to create circles and lines, show them on the screen, and change their positions.
These skills can help you with other CodeHS graphics and animation exercises.
Final Tips for Completing the CodeHS Leash Exercise
The 9.7.4 Leash CodeHS exercise teaches you how to make a ball follow your mouse while staying connected to a line.
Remember the main steps: create the ball, create the line, add both to the screen, and use a mouse event to update their positions.
If something does not work, check your variable names, mouse event, and line position.
Most importantly, try to understand what each line of code does. This will make it easier to complete other JavaScript graphics and animation exercises in CodeHS.
Frequently Asked Questions About 9.7.4 Leash CodeHS Answers
What Is the Answer to 9.7.4 Leash in CodeHS?
The exercise uses a ball and a line. The ball follows the mouse, while the line changes its endpoint to stay connected to the ball.
What Does the Leash Exercise Teach?
It teaches JavaScript graphics, mouse events, variables, functions, and screen positions.
Why Does My CodeHS Leash Line Disappear?
Check that you created the line, added it to the screen, and used the correct coordinates. Also, make sure the line variable is not changed by mistake.
Should I Use mouseMoveMethod() or mouseDragMethod()?
Use mouseMoveMethod() for normal mouse movement. Use mouseDragMethod() when the mouse must move while a button is held down.
Why Do I Need Global Variables for the Ball and Line?
Global variables let different functions use the same ball and line. This helps the program move the objects without creating new ones each time.
Is 9.7.4 Leash the Same as 4.7.4 Leash?
The Leash exercise appears under different numbers in different CodeHS courses. Check your course instructions to find the correct version.
You Could Also Like:
Image Search Techniques: A Simple Guide to Finding, Tracing, and Checking Images
Who Owns Pinterest? Meet Its Founders and Major Shareholders
Disclaimer:
This article is for informational and educational purposes only. The code examples are shared to help students learn and understand JavaScript. Always check your own CodeHS lesson instructions. Some images may be AI-generated for illustration. All copyrights and trademarks belong to their respective owners.
Victor Rennard is a multi-niche writer for BaddieHubMagazine.com, creating clear and engaging articles across lifestyle, trends, tech, entertainment, and general guides. With a simple, helpful writing style, Victor aims to give readers quick insights and reliable information on topics that matter.