Back to AI Flashcard MakerInformation Technology /Animation and Games CodeHS Part 4

Animation and Games CodeHS Part 4

Information Technology10 CardsCreated 5 months ago

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

Tap or swipe ↕ to flip
Swipe ←→Navigate
1/10

Key Terms

Term
Definition

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
TermDefinition

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, 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?
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

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:
var ball;
function start(){
ball = new Circle(40);
add(ball);
setTimer(draw, 20);
mouseClickMethod(stopAnimation);
function draw(){
ball.move(2, 2);
}
function stopAnimation(e){
//your code here ...
}
Which statement would stop the animation in this program?

stopTimer(draw);

We extend our draw function to include:
1 var ball;
2 var moveCounter = 0;
3 function draw(){
4
5 ball.move(2, 2);
6 moveCounter++;
7
8 if(moveCounter == 25){
9 stopTimer(draw);
10 }
11 }
12
13 function start(){
14 ball = new Circle(40);
15 add(ball);
14 setTimer(draw, 20);
15 }
How many times will the ball be moved before the animation stops?

25

Which of the following are techniques that make our code more reusable?
I - Using constants instead of magic numbers, II - Using specific values in our functions rather than using parameters, III - Writing multiple functions that solve small subproblems rather than one function that solves the entire problem. IV - Writing as few lines of code as possible

I and III, and IV

Which function has better reusability?

function drawCircle(x, y, radius, color){
var circle = new Circle(radius);
circle.setPosition(x, y);
circle.setColor(color);
add(circle);
}