Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.javascript > #40782
| Path | csiph.com!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail |
|---|---|
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
| Newsgroups | comp.lang.javascript |
| Subject | Re: How come I don't get a box when I add an eventlistener? |
| Date | Thu, 10 Feb 2022 19:47:04 +0000 |
| Organization | A noiseless patient Spider |
| Lines | 115 |
| Message-ID | <874k56ld1j.fsf@bsb.me.uk> (permalink) |
| References | <24969684-2577-4eb1-b316-0b490769f615n@googlegroups.com> <87y22jlnaa.fsf@bsb.me.uk> <2905cd2e-2dd1-470d-96d9-bb3ac82b45aan@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain |
| Injection-Info | reader02.eternal-september.org; posting-host="44ac79418358714662faf2c71e7e4cc5"; logging-data="12844"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1//ciyVifk9wo8/QS7eWnfBY+fUpHalYv8=" |
| Cancel-Lock | sha1:z3dHzoY9vV20kdGvvgPucqvTolM= sha1:uuBoY5Ezb5WZc82rAjArj//gx5c= |
| X-BSB-Auth | 1.f58fccb10e3a2e91875c.20220210194704GMT.874k56ld1j.fsf@bsb.me.uk |
| Xref | csiph.com comp.lang.javascript:40782 |
Show key headers only | View raw
chad altenburg <cdalten@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 black
>> > box. However, when I don't, I don't get a black box. Why?
>> Because there is a syntax error. The code can't run if the interpreter
>> 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() {
>> > document.addEventListener('keydown', function(e)) {
>> That second ) is in the wrong place.
>> > switch(e.keycode) {
>> > case 37: break; //left
>> > case 38: break; //up
>> > case 39: break; //right
>> > }
>> > }
>> It should be here (along with a semicolon)..
>> > }
>> >
>> >
>> >
>> > //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>
--
Ben.
Back to comp.lang.javascript | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
How come I don't get a box when I add an eventlistener? chad altenburg <cdalten@gmail.com> - 2022-02-09 09:42 -0800
Re: How come I don't get a box when I add an eventlistener? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-02-09 21:53 +0000
Re: How come I don't get a box when I add an eventlistener? chad altenburg <cdalten@gmail.com> - 2022-02-10 10:18 -0800
Re: How come I don't get a box when I add an eventlistener? Ben Bacarisse <ben.usenet@bsb.me.uk> - 2022-02-10 19:47 +0000
Re: How come I don't get a box when I add an eventlistener? chad altenburg <cdalten@gmail.com> - 2022-02-10 19:13 -0800
Re: How come I don't get a box when I add an eventlistener? The Natural Philosopher <tnp@invalid.invalid> - 2022-02-11 17:37 +0000
Re: How come I don't get a box when I add an eventlistener? Arno Welzel <usenet@arnowelzel.de> - 2022-02-12 18:05 +0100
Re: How come I don't get a box when I add an eventlistener? Arno Welzel <usenet@arnowelzel.de> - 2022-02-10 14:55 +0100
csiph-web