Created this simple code that should move a rect in p5.js. I get the error ReferenceError: keyDown is not defined. What's wrong? Do I have to install any other libraries or is it a syntax error?
function setup() {
createCanvas(400, 400);
}
let x = 0;
let y = 0;
function draw() {
if (keyDown(68)) // d
{
x += 3
}
if (keyDown(65)) // a
{
x -= 3
}
if (keyDown(87))
{
y -= 3
}
if (keyDown(83))
{
y += 3
}
background(220)
rect(x, y, 30, 50);
}
Syntax error. The correct way to check for a keypress is KeyIsDown(keycode):
function setup() {
createCanvas(400, 400);
}
let x = 0;
let y = 0;
function draw() {
if (keyIsDown(68)) // d
{
x += 3
}
if (keyIsDown(65)) // a
{
x -= 3
}
if (keyIsDown(87))
{
y -= 3
}
if (keyIsDown(83))
{
y += 3
}
background(220)
rect(x, y, 30, 50);
}
Solved by @Matei Piele with keyIsDown(), posting this so other people know, can also be solved with keyIsPressed().