When I comment out the event listener code, I get a black
box. However, when I don't, I don't get a black box. Why?
<html>
<canvas id = "canvas"></canvas>
<script>
var x = 20;
var y = 20;
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var i =0;
addEventListener();
function drawSnake()
{
for (i = 0; i < 20; i++) {
ctx.fillRect(x, y, 25, 25);
}
}
drawSnake();
function addListener() {
document.addEventListener('keydown', function(e)) {
switch(e.keycode) {
case 37: break; //left
case 38: break; //up
case 39: break; //right
}
}
}
//ctx.fillRect(50, 100, 150, 150);
// }
</script>
</html>
When I comment out the event listener code, I get a black box. However, when I don't, I don't get a black box. Why?
chad altenburg <cda...@gmail.com> writes:
When I comment out the event listener code, I get a blackBecause there is a syntax error. The code can't run if the interpreter
box. However, when I don't, I don't get a black box. Why?
can't make sense of what you've written.
<html>
<canvas id = "canvas"></canvas>
<script>
var x = 20;
var y = 20;
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var i =0;
addEventListener();Remove this as well. It will cause a run-time error.
function drawSnake()
{
for (i = 0; i < 20; i++) {
ctx.fillRect(x, y, 25, 25);
}
}
drawSnake();
function addListener() {That second ) is in the wrong place.
document.addEventListener('keydown', function(e)) {
switch(e.keycode) {It should be here (along with a semicolon)..
case 37: break; //left
case 38: break; //up
case 39: break; //right
}
}
}
//ctx.fillRect(50, 100, 150, 150);
// }
</script>--
</html>
On Wednesday, February 9, 2022 at 1:53:46 PM UTC-8, Ben Bacarisse wrote:
chad altenburg <cda...@gmail.com> writes:
When I comment out the event listener code, I get a blackBecause there is a syntax error. The code can't run if the interpreter
box. However, when I don't, I don't get a black box. Why?
can't make sense of what you've written.
<html>Remove this as well. It will cause a run-time error.
<canvas id = "canvas"></canvas>
<script>
var x = 20;
var y = 20;
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var i =0;
addEventListener();
function drawSnake()That second ) is in the wrong place.
{
for (i = 0; i < 20; i++) {
ctx.fillRect(x, y, 25, 25);
}
}
drawSnake();
function addListener() {
document.addEventListener('keydown', function(e)) {
switch(e.keycode) {It should be here (along with a semicolon)..
case 37: break; //left
case 38: break; //up
case 39: break; //right
}
}
}--
//ctx.fillRect(50, 100, 150, 150);
// }
</script>
</html>
And I still don't get why it's not detecting the right arrow key...
<html>
<canvas id = "canvas" width = "400" height="400"></canvas>
<script>
var x = 20;
var y = 20;
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var i =0;
//addEventListener();
function main() {
drawSnake();
addEventListener();
}
main();
function drawSnake()
{
for (i = 0; i < 20; i++) {
ctx.fillRect(x, y, 25, 25);
}
}
function addListener() {
document.addEventListener('keydown', function(e){
switch(e.keyCode) {
case 37: break; //left
case 38:
//ctx.fillRect(x, y + 10, 25, 25)
break; //up
case 39:
ctx.fillRect(x + 10, y, 25, 25);
alert("key moved right");
break; //right
case 40: break;
}
});
}
//ctx.fillRect(50, 100, 150, 150);
// }
</script>
</html>
chad altenburg <cda...@gmail.com> writes:
On Wednesday, February 9, 2022 at 1:53:46 PM UTC-8, Ben Bacarisse wrote:
chad altenburg <cda...@gmail.com> writes:
When I comment out the event listener code, I get a blackBecause there is a syntax error. The code can't run if the interpreter
box. However, when I don't, I don't get a black box. Why?
can't make sense of what you've written.
<html>Remove this as well. It will cause a run-time error.
<canvas id = "canvas"></canvas>
<script>
var x = 20;
var y = 20;
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var i =0;
addEventListener();
function drawSnake()That second ) is in the wrong place.
{
for (i = 0; i < 20; i++) {
ctx.fillRect(x, y, 25, 25);
}
}
drawSnake();
function addListener() {
document.addEventListener('keydown', function(e)) {
switch(e.keycode) {It should be here (along with a semicolon)..
case 37: break; //left
case 38: break; //up
case 39: break; //right
}
}
}--
//ctx.fillRect(50, 100, 150, 150);
// }
</script>
</html>
And I still don't get why it's not detecting the right arrow key...It still has errors, and you still have mixed up names.
You can't program like this. You need to learn to find this sort of
basic error for yourself or you will be posting here 20 times a day.
Find out how to get help for yourself. Do you know how to view the
console in your browser?
<html>
<canvas id = "canvas" width = "400" height="400"></canvas>
<script>
var x = 20;
var y = 20;
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
var i =0;
//addEventListener();
function main() {
drawSnake();
addEventListener();
}
main();
function drawSnake()
{
for (i = 0; i < 20; i++) {
ctx.fillRect(x, y, 25, 25);
}
}
function addListener() {
document.addEventListener('keydown', function(e){
switch(e.keyCode) {
case 37: break; //left
case 38:
//ctx.fillRect(x, y + 10, 25, 25)
break; //up
case 39:
ctx.fillRect(x + 10, y, 25, 25);
alert("key moved right");
break; //right
case 40: break;
}
});
}
//ctx.fillRect(50, 100, 150, 150);
// }
</script>--
</html>
On Thursday, February 10, 2022 at 11:47:17 AM UTC-8, Ben Bacarisse wrote:
And I still don't get why it's not detecting the right arrow key...It still has errors, and you still have mixed up names.
You can't program like this. You need to learn to find this sort of
basic error for yourself or you will be posting here 20 times a day.
Find out how to get help for yourself. Do you know how to view the
console in your browser?
With all due respect, go fuck yourself.
On Thursday, February 10, 2022 at 11:47:17 AM UTC-8, Ben Bacarisse wrote:[...]
You can't program like this. You need to learn to find this sort of
basic error for yourself or you will be posting here 20 times a day.
Find out how to get help for yourself. Do you know how to view the
console in your browser?
With all due respect, go fuck yourself.
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 469 |
Nodes: | 16 (2 / 14) |
Uptime: | 42:22:03 |
Calls: | 9,449 |
Calls today: | 6 |
Files: | 13,596 |
Messages: | 6,111,762 |
Posted today: | 1 |