How to move square with keys in javascript?2019 Community Moderator ElectionIssues with keyIsDown() in p5jsHow to validate an email address in JavaScript?How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I redirect to another webpage?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?

Which classes are needed to have access to every spell in the PHB?

Is it possible to find 2014 distinct positive integers whose sum is divisible by each of them?

Specifying a starting column with colortbl package and xcolor

Giving a career talk in my old university, how prominently should I tell students my salary?

Why does cron require MTA for logging?

Signed and unsigned numbers

Can we track matter through time by looking at different depths in space?

How do spaceships determine each other's mass in space?

Plausibility of Mushroom Buildings

Gaining more land

Outlet with 3 sets of wires

I reported the illegal activity of my boss to his boss. My boss found out. Now I am being punished. What should I do?

Source permutation

How can I manipulate the output of Information?

What is this diamond of every day?

What would be the most expensive material to an intergalactic society?

How to design an organic heat-shield?

Why does Solve lock up when trying to solve the quadratic equation with large integers?

Should I take out a loan for a friend to invest on my behalf?

What is better: yes / no radio, or simple checkbox?

Can I negotiate a patent idea for a raise, under French law?

Confusion about Complex Continued Fraction

School performs periodic password audits. Is my password compromised?

Proving a statement about real numbers



How to move square with keys in javascript?



2019 Community Moderator ElectionIssues with keyIsDown() in p5jsHow to validate an email address in JavaScript?How do JavaScript closures work?How do I check if an element is hidden in jQuery?How do I remove a property from a JavaScript object?Which equals operator (== vs ===) should be used in JavaScript comparisons?How do I redirect to another webpage?How do I include a JavaScript file in another JavaScript file?What does “use strict” do in JavaScript, and what is the reasoning behind it?How to check whether a string contains a substring in JavaScript?How do I remove a particular element from an array in JavaScript?










4















I'm a beginner programmer literally trying to code for the first time after watching some Coding Train videos and I am simply trying to move a square around the screen using the keys.



I have what I thought is the right way to do it up oversimplified like y = y + 20; instead of y += 20; but it still won't work.



Any help would be greatly appreciated.






var y = 200;
var x = 200;

function setup()
createCanvas(400, 400);


function draw()
background(220);
rect(x, y, 20, 20);


function keyPressed()
if (keyCode === UP_ARROW)
y = y + 10;
console.log("help")
else
return false;



<script src="//cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.3/p5.min.js"></script>












share|improve this question









New contributor




Jagger Denhof is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1





    paste code instead of image.And add draw() next line of y=y+20

    – prasanth
    Mar 6 at 4:36












  • I thought I did post the code and when you say next to again I have no idea what I'm doing so could explain more?

    – Jagger Denhof
    Mar 6 at 4:43











  • i m not familer with p5.js.just call the draw() function inside the keypress the after y value updated.It will redraw the position of rectangle

    – prasanth
    Mar 6 at 4:46












  • Take a look at this answer: stackoverflow.com/a/54811918/1978785 It has some extra information about responding to key strokes

    – Charlie Wallace
    Mar 6 at 13:45















4















I'm a beginner programmer literally trying to code for the first time after watching some Coding Train videos and I am simply trying to move a square around the screen using the keys.



I have what I thought is the right way to do it up oversimplified like y = y + 20; instead of y += 20; but it still won't work.



Any help would be greatly appreciated.






var y = 200;
var x = 200;

function setup()
createCanvas(400, 400);


function draw()
background(220);
rect(x, y, 20, 20);


function keyPressed()
if (keyCode === UP_ARROW)
y = y + 10;
console.log("help")
else
return false;



<script src="//cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.3/p5.min.js"></script>












share|improve this question









New contributor




Jagger Denhof is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.















  • 1





    paste code instead of image.And add draw() next line of y=y+20

    – prasanth
    Mar 6 at 4:36












  • I thought I did post the code and when you say next to again I have no idea what I'm doing so could explain more?

    – Jagger Denhof
    Mar 6 at 4:43











  • i m not familer with p5.js.just call the draw() function inside the keypress the after y value updated.It will redraw the position of rectangle

    – prasanth
    Mar 6 at 4:46












  • Take a look at this answer: stackoverflow.com/a/54811918/1978785 It has some extra information about responding to key strokes

    – Charlie Wallace
    Mar 6 at 13:45













4












4








4








I'm a beginner programmer literally trying to code for the first time after watching some Coding Train videos and I am simply trying to move a square around the screen using the keys.



I have what I thought is the right way to do it up oversimplified like y = y + 20; instead of y += 20; but it still won't work.



Any help would be greatly appreciated.






var y = 200;
var x = 200;

function setup()
createCanvas(400, 400);


function draw()
background(220);
rect(x, y, 20, 20);


function keyPressed()
if (keyCode === UP_ARROW)
y = y + 10;
console.log("help")
else
return false;



<script src="//cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.3/p5.min.js"></script>












share|improve this question









New contributor




Jagger Denhof is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.












I'm a beginner programmer literally trying to code for the first time after watching some Coding Train videos and I am simply trying to move a square around the screen using the keys.



I have what I thought is the right way to do it up oversimplified like y = y + 20; instead of y += 20; but it still won't work.



Any help would be greatly appreciated.






var y = 200;
var x = 200;

function setup()
createCanvas(400, 400);


function draw()
background(220);
rect(x, y, 20, 20);


function keyPressed()
if (keyCode === UP_ARROW)
y = y + 10;
console.log("help")
else
return false;



<script src="//cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.3/p5.min.js"></script>








var y = 200;
var x = 200;

function setup()
createCanvas(400, 400);


function draw()
background(220);
rect(x, y, 20, 20);


function keyPressed()
if (keyCode === UP_ARROW)
y = y + 10;
console.log("help")
else
return false;



<script src="//cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.3/p5.min.js"></script>





var y = 200;
var x = 200;

function setup()
createCanvas(400, 400);


function draw()
background(220);
rect(x, y, 20, 20);


function keyPressed()
if (keyCode === UP_ARROW)
y = y + 10;
console.log("help")
else
return false;



<script src="//cdnjs.cloudflare.com/ajax/libs/p5.js/0.3.3/p5.min.js"></script>






javascript p5.js






share|improve this question









New contributor




Jagger Denhof is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.











share|improve this question









New contributor




Jagger Denhof is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









share|improve this question




share|improve this question








edited Mar 6 at 4:49







Jagger Denhof













New contributor




Jagger Denhof is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.









asked Mar 6 at 4:34









Jagger DenhofJagger Denhof

213




213




New contributor




Jagger Denhof is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.





New contributor





Jagger Denhof is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.






Jagger Denhof is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







  • 1





    paste code instead of image.And add draw() next line of y=y+20

    – prasanth
    Mar 6 at 4:36












  • I thought I did post the code and when you say next to again I have no idea what I'm doing so could explain more?

    – Jagger Denhof
    Mar 6 at 4:43











  • i m not familer with p5.js.just call the draw() function inside the keypress the after y value updated.It will redraw the position of rectangle

    – prasanth
    Mar 6 at 4:46












  • Take a look at this answer: stackoverflow.com/a/54811918/1978785 It has some extra information about responding to key strokes

    – Charlie Wallace
    Mar 6 at 13:45












  • 1





    paste code instead of image.And add draw() next line of y=y+20

    – prasanth
    Mar 6 at 4:36












  • I thought I did post the code and when you say next to again I have no idea what I'm doing so could explain more?

    – Jagger Denhof
    Mar 6 at 4:43











  • i m not familer with p5.js.just call the draw() function inside the keypress the after y value updated.It will redraw the position of rectangle

    – prasanth
    Mar 6 at 4:46












  • Take a look at this answer: stackoverflow.com/a/54811918/1978785 It has some extra information about responding to key strokes

    – Charlie Wallace
    Mar 6 at 13:45







1




1





paste code instead of image.And add draw() next line of y=y+20

– prasanth
Mar 6 at 4:36






paste code instead of image.And add draw() next line of y=y+20

– prasanth
Mar 6 at 4:36














I thought I did post the code and when you say next to again I have no idea what I'm doing so could explain more?

– Jagger Denhof
Mar 6 at 4:43





I thought I did post the code and when you say next to again I have no idea what I'm doing so could explain more?

– Jagger Denhof
Mar 6 at 4:43













i m not familer with p5.js.just call the draw() function inside the keypress the after y value updated.It will redraw the position of rectangle

– prasanth
Mar 6 at 4:46






i m not familer with p5.js.just call the draw() function inside the keypress the after y value updated.It will redraw the position of rectangle

– prasanth
Mar 6 at 4:46














Take a look at this answer: stackoverflow.com/a/54811918/1978785 It has some extra information about responding to key strokes

– Charlie Wallace
Mar 6 at 13:45





Take a look at this answer: stackoverflow.com/a/54811918/1978785 It has some extra information about responding to key strokes

– Charlie Wallace
Mar 6 at 13:45












1 Answer
1






active

oldest

votes


















3














You should get into the habit of looking at your developer tools for errors. You'll see that you're getting an error:



Uncaught ReferenceError: keycode is not defined (sketch: line 14)


If you consult the P5.js reference you'll see that it should be keyCode instead of keycode.



But even if you fix that, you're going to have another problem.



Your best friend is the P5.js reference which explains:




The variable keyCode is used to detect special keys such as BACKSPACE, DELETE, ENTER, RETURN, TAB, ESCAPE, SHIFT, CONTROL, OPTION, ALT, UP_ARROW, DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW.




You could figure out what the code is for the W key, but it's probably easier to just use the key variable instead.



Also note that because you're checking against 'W' you'll have to hold in shift to type an upper-case letter. You might consider using 'w' instead.



Shameless self-promotion: here are some tutorial on P5.js.






share|improve this answer























  • Thanks for the feedback. I fixed my mistakes but still, nothing happens. I'll update the code after I'm done typing this.

    – Jagger Denhof
    Mar 6 at 4:48











  • Could it be that I am doing this on the web editor instead of the running from the separate tab thing like Daniel does in coding train.

    – Jagger Denhof
    Mar 6 at 5:01











  • @JaggerDenhof I'm using the web editor as well. Maybe make sure the canvas is focused by clicking on it before typing any letters? Also try debugging your code: maybe add extra print statements to understand better what's going on?

    – Kevin Workman
    Mar 6 at 5:07











  • lol i literally had to click the screen. I'm dumb. Thx a lot.

    – Jagger Denhof
    Mar 6 at 5:10










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);






Jagger Denhof is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55015630%2fhow-to-move-square-with-keys-in-javascript%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














You should get into the habit of looking at your developer tools for errors. You'll see that you're getting an error:



Uncaught ReferenceError: keycode is not defined (sketch: line 14)


If you consult the P5.js reference you'll see that it should be keyCode instead of keycode.



But even if you fix that, you're going to have another problem.



Your best friend is the P5.js reference which explains:




The variable keyCode is used to detect special keys such as BACKSPACE, DELETE, ENTER, RETURN, TAB, ESCAPE, SHIFT, CONTROL, OPTION, ALT, UP_ARROW, DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW.




You could figure out what the code is for the W key, but it's probably easier to just use the key variable instead.



Also note that because you're checking against 'W' you'll have to hold in shift to type an upper-case letter. You might consider using 'w' instead.



Shameless self-promotion: here are some tutorial on P5.js.






share|improve this answer























  • Thanks for the feedback. I fixed my mistakes but still, nothing happens. I'll update the code after I'm done typing this.

    – Jagger Denhof
    Mar 6 at 4:48











  • Could it be that I am doing this on the web editor instead of the running from the separate tab thing like Daniel does in coding train.

    – Jagger Denhof
    Mar 6 at 5:01











  • @JaggerDenhof I'm using the web editor as well. Maybe make sure the canvas is focused by clicking on it before typing any letters? Also try debugging your code: maybe add extra print statements to understand better what's going on?

    – Kevin Workman
    Mar 6 at 5:07











  • lol i literally had to click the screen. I'm dumb. Thx a lot.

    – Jagger Denhof
    Mar 6 at 5:10















3














You should get into the habit of looking at your developer tools for errors. You'll see that you're getting an error:



Uncaught ReferenceError: keycode is not defined (sketch: line 14)


If you consult the P5.js reference you'll see that it should be keyCode instead of keycode.



But even if you fix that, you're going to have another problem.



Your best friend is the P5.js reference which explains:




The variable keyCode is used to detect special keys such as BACKSPACE, DELETE, ENTER, RETURN, TAB, ESCAPE, SHIFT, CONTROL, OPTION, ALT, UP_ARROW, DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW.




You could figure out what the code is for the W key, but it's probably easier to just use the key variable instead.



Also note that because you're checking against 'W' you'll have to hold in shift to type an upper-case letter. You might consider using 'w' instead.



Shameless self-promotion: here are some tutorial on P5.js.






share|improve this answer























  • Thanks for the feedback. I fixed my mistakes but still, nothing happens. I'll update the code after I'm done typing this.

    – Jagger Denhof
    Mar 6 at 4:48











  • Could it be that I am doing this on the web editor instead of the running from the separate tab thing like Daniel does in coding train.

    – Jagger Denhof
    Mar 6 at 5:01











  • @JaggerDenhof I'm using the web editor as well. Maybe make sure the canvas is focused by clicking on it before typing any letters? Also try debugging your code: maybe add extra print statements to understand better what's going on?

    – Kevin Workman
    Mar 6 at 5:07











  • lol i literally had to click the screen. I'm dumb. Thx a lot.

    – Jagger Denhof
    Mar 6 at 5:10













3












3








3







You should get into the habit of looking at your developer tools for errors. You'll see that you're getting an error:



Uncaught ReferenceError: keycode is not defined (sketch: line 14)


If you consult the P5.js reference you'll see that it should be keyCode instead of keycode.



But even if you fix that, you're going to have another problem.



Your best friend is the P5.js reference which explains:




The variable keyCode is used to detect special keys such as BACKSPACE, DELETE, ENTER, RETURN, TAB, ESCAPE, SHIFT, CONTROL, OPTION, ALT, UP_ARROW, DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW.




You could figure out what the code is for the W key, but it's probably easier to just use the key variable instead.



Also note that because you're checking against 'W' you'll have to hold in shift to type an upper-case letter. You might consider using 'w' instead.



Shameless self-promotion: here are some tutorial on P5.js.






share|improve this answer













You should get into the habit of looking at your developer tools for errors. You'll see that you're getting an error:



Uncaught ReferenceError: keycode is not defined (sketch: line 14)


If you consult the P5.js reference you'll see that it should be keyCode instead of keycode.



But even if you fix that, you're going to have another problem.



Your best friend is the P5.js reference which explains:




The variable keyCode is used to detect special keys such as BACKSPACE, DELETE, ENTER, RETURN, TAB, ESCAPE, SHIFT, CONTROL, OPTION, ALT, UP_ARROW, DOWN_ARROW, LEFT_ARROW, RIGHT_ARROW.




You could figure out what the code is for the W key, but it's probably easier to just use the key variable instead.



Also note that because you're checking against 'W' you'll have to hold in shift to type an upper-case letter. You might consider using 'w' instead.



Shameless self-promotion: here are some tutorial on P5.js.







share|improve this answer












share|improve this answer



share|improve this answer










answered Mar 6 at 4:41









Kevin WorkmanKevin Workman

34.1k54172




34.1k54172












  • Thanks for the feedback. I fixed my mistakes but still, nothing happens. I'll update the code after I'm done typing this.

    – Jagger Denhof
    Mar 6 at 4:48











  • Could it be that I am doing this on the web editor instead of the running from the separate tab thing like Daniel does in coding train.

    – Jagger Denhof
    Mar 6 at 5:01











  • @JaggerDenhof I'm using the web editor as well. Maybe make sure the canvas is focused by clicking on it before typing any letters? Also try debugging your code: maybe add extra print statements to understand better what's going on?

    – Kevin Workman
    Mar 6 at 5:07











  • lol i literally had to click the screen. I'm dumb. Thx a lot.

    – Jagger Denhof
    Mar 6 at 5:10

















  • Thanks for the feedback. I fixed my mistakes but still, nothing happens. I'll update the code after I'm done typing this.

    – Jagger Denhof
    Mar 6 at 4:48











  • Could it be that I am doing this on the web editor instead of the running from the separate tab thing like Daniel does in coding train.

    – Jagger Denhof
    Mar 6 at 5:01











  • @JaggerDenhof I'm using the web editor as well. Maybe make sure the canvas is focused by clicking on it before typing any letters? Also try debugging your code: maybe add extra print statements to understand better what's going on?

    – Kevin Workman
    Mar 6 at 5:07











  • lol i literally had to click the screen. I'm dumb. Thx a lot.

    – Jagger Denhof
    Mar 6 at 5:10
















Thanks for the feedback. I fixed my mistakes but still, nothing happens. I'll update the code after I'm done typing this.

– Jagger Denhof
Mar 6 at 4:48





Thanks for the feedback. I fixed my mistakes but still, nothing happens. I'll update the code after I'm done typing this.

– Jagger Denhof
Mar 6 at 4:48













Could it be that I am doing this on the web editor instead of the running from the separate tab thing like Daniel does in coding train.

– Jagger Denhof
Mar 6 at 5:01





Could it be that I am doing this on the web editor instead of the running from the separate tab thing like Daniel does in coding train.

– Jagger Denhof
Mar 6 at 5:01













@JaggerDenhof I'm using the web editor as well. Maybe make sure the canvas is focused by clicking on it before typing any letters? Also try debugging your code: maybe add extra print statements to understand better what's going on?

– Kevin Workman
Mar 6 at 5:07





@JaggerDenhof I'm using the web editor as well. Maybe make sure the canvas is focused by clicking on it before typing any letters? Also try debugging your code: maybe add extra print statements to understand better what's going on?

– Kevin Workman
Mar 6 at 5:07













lol i literally had to click the screen. I'm dumb. Thx a lot.

– Jagger Denhof
Mar 6 at 5:10





lol i literally had to click the screen. I'm dumb. Thx a lot.

– Jagger Denhof
Mar 6 at 5:10












Jagger Denhof is a new contributor. Be nice, and check out our Code of Conduct.









draft saved

draft discarded


















Jagger Denhof is a new contributor. Be nice, and check out our Code of Conduct.












Jagger Denhof is a new contributor. Be nice, and check out our Code of Conduct.











Jagger Denhof is a new contributor. Be nice, and check out our Code of Conduct.














Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55015630%2fhow-to-move-square-with-keys-in-javascript%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

1928 у кіно

Захаров Федір Захарович

Ель Греко