Anthony Boyac
Anthony Boyac

Reputation: 17

Key typed on khan academy

I need to write a code on Khan Academy that lets me change images I have coded by pressing a key. To do this, I need to use keyTyped function, but I don't know how to make it work.

Also, I cannot use loops; I just need to write a function that lets me pick between different images by pressing a key. Below is a code of one image that I have.

//image: Kishin
var x = 180;
var y = 170;
var widthEllipse = 300;
var heightEllipse = 300;
var widthX = 80;
var heightY = 180;

background(94, 30, 30);
// bigger ellipse
fill(0, 0, 0);
ellipse(x + 20, y + 20, widthEllipse, heightEllipse);

//smaller ellipses
fill(148, 0, 0);
ellipse(130, 150, widthX - 50, heightY - 40);
ellipse(200, 230, widthX - 50, heightY - 40);
ellipse(270, 150, widthX - 50, heightY - 40);

// smallest ellipses
fill(0, 0, 0);
ellipse(130, 150, 30, 35);
ellipse(200, 230, 30, 35);
ellipse(270, 150, 30, 35);

Upvotes: 0

Views: 309

Answers (2)

Binit Shah
Binit Shah

Reputation: 81

The keyTyped function is called whenever a key is pressed. So from there, you can check what key was typed using the variable: "key". Each key on your keyboard is associated with a number according to the ASCII table and "key" holds the number of the key pressed. Example code would look like:

keyTyped = function() { if(key === 40) { //down key background(255, 255, 255); //clear the screen //draw image } else if.... //add more keys for the other images }

Upvotes: 0

zuzurino
zuzurino

Reputation: 13

Khan-Academy uses a JavaScript library called p5.js

I suggest you check out the documentation for the keyTyped() if you are unsure of its correct usage.

p5.js reference for keyTyped()

Upvotes: 0

Related Questions