Animation and Games CodeHS Part 4
This deck covers key concepts and questions from the Animation and Games CodeHS Part 4, focusing on global variables, collision detection, drag and drop, animation, and reusability.
Which of the following are good examples of things that should be stored as global variables?
I - A ball for a game that is used in multiple functions
II - A counter that keeps track of how many times the user has clicked the mouse
III - A for loop counter variable
IV - The color of a rectangle that is only used in one function
I and II only
Key Terms
Which of the following are good examples of things that should be stored as global variables?
I - A ball for a game that is used in multiple functions
II - A counter that keeps track of how many times the user has clicked the mouse
III - A for loop counter variable
IV - The color of a rectangle that is only used in one function
I and II only
In the following code
function start(){
mouseClickMethod(clickHandler);
}
function clickHandler(e){
//your code here
}
How can we get the graphical element at the location of the user click?
var elem = getElementAt(e.getX(), e.getY());
9.9.7: Click for Collision
The code creates a simple animation with a blue and red circle that can collide. It defines constants for circle size, m...
9.9.8: Drag and Drop
The code creates a simple drag-and-drop program with three randomly colored circles. drawCircles() generates the circles...
What does this program do?
var ball;
function start(){
ball = new Circle(40);
add(ball);
setTimer(draw, 20);
}
function draw(){
ball.move(2, 2);
}
Animates a ball by moving it down and to the right once every 20 milliseconds.
In the following code:
var ball;
function start(){
ball = new Circle(40);
add(ball);
setTimer(draw, 20);
}
function draw(){
ball.move(2, 2);
}
Which of the following statements are true about ball?
I - ball is a local variable, II - the ball variable in draw is different from the ball variable in start, III - ball is a global variable, IV - ball's scope includes both start and draw
III and IV
Related Flashcard Decks
Study Tips
- Press F to enter focus mode for distraction-free studying
- Review cards regularly to improve retention
- Try to recall the answer before flipping the card
- Share this deck with friends to study together
| Term | Definition |
|---|---|
Which of the following are good examples of things that should be stored as global variables? | I and II only |
In the following code | var elem = getElementAt(e.getX(), e.getY()); |
9.9.7: Click for Collision | The code creates a simple animation with a blue and red circle that can collide. It defines constants for circle size, movement speed, and delay. start() initializes the circles and sets a timer for movement while enabling mouse clicks to trigger collisions. blueCircle() and redCircle() create and position the circles on the canvas. collide() is meant to handle movement by repeatedly calling blueCircle() on a timer. pause() toggles the animation on or off. However, the code has errors: blueCircle and redCircle are functions, not variables, and timers are set incorrectly. Proper collision handling and movement logic are missing, so clicking currently just calls collide(). |
9.9.8: Drag and Drop | The code creates a simple drag-and-drop program with three randomly colored circles. drawCircles() generates the circles at random positions on the canvas. start() initializes the circles and sets up mouse event handlers. drag(e) selects the circle under the mouse when pressed. up(e) moves the selected circle to follow the mouse during dragging. drop(e) releases the circle when the mouse is released. newPosition stores the currently dragged circle. The program allows the user to click, drag, and drop any circle anywhere on the canvas. |
What does this program do? | Animates a ball by moving it down and to the right once every 20 milliseconds. |
In the following code: | III and IV |
The following program animates a ball by setting a timer to move the ball across the screen. We want the animation to stop whenever the user clicks the mouse: | stopTimer(draw); |
We extend our draw function to include: | 25 |
Which of the following are techniques that make our code more reusable? | I and III, and IV |
Which function has better reusability? | function drawCircle(x, y, radius, color){ |