Lesson 12 Bubble 2


#1

Seems like a dumb question, but where is the text set to “True” and “False”?


#2

The keyDown() block is an event handler.This block is part of the Javascript language. I found this description on W3schools: https://www.w3schools.com/js/js_events.asp

Event handlers can be used to handle, and verify, user input, user actions, and browser actions:

Things that should be done every time a page loads
Things that should be done when the page is closed
Action that should be performed when a user clicks a button
Content that should be verified when a user inputs data


#3

Thanks for the reply. I understand about event handlers. What I don’t see are writing the text “true” false" in the code:

textAlign(“center”, “center”);
textSize(100);
text(keyDown(“up”), 200, 200);

Doesn’t the last line refer to the x,y coordinate? I am sure that my students will ask this and I won’t have an answer.


#4

@nread

Hi Nancy,

The “true”/“false” text comes from the keyDown() block. That block returns either true or false, depending on whether the key is pressed down. The text block prints that value to the screen.

text(keyDown("up"), 200, 200);

For example, if the up arrow is pressed down, the above line of code evaluates keyDown("up") to true, then prints “true” to the screen at (200, 200), the (x,y) coordinates that you mentioned in your post.

Normally we use true/false (Booleans) in conditionals, such as using an if statement to move a character up the screen only when keyDown("up") evaluates to true. In Javascript, Booleans can also be printed to the screen as if they were text. That’s what’s happening here. It’s a little odd to use Booleans in this way, but it’s the most straightforward way to see what keyDown() is returning.

Elizabeth


#5

Got it. Thank you for the very clear explanation. I suspected as much but wanted to make sure before I told my students. I am not sure when they are asked to predict what happens with the code that they will answer “true” or “false.”