Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

HTML

Manuel Schulze
Manuel Schulze
12,739 Points

Adding KeyListener to HTML5/LimeJS Game

Hey guys,

I've recently watched the workshop "Coding Your First HTML5 Game". I've decided to extend the project a little. Now I've tried to add some Keyevent Listeners but it doesn't work for me. I hope someone may help me.

This is my markup:

goog.events.listen(goog.global, ['keydown', 'keyup'], function(e) {
        switch (e.keyCode) {
            case 37: //LEFT
                this.frog.startMovement(WEST);
                break;
            case 65: //A
                this.frog.startMovement(WEST);
                break;
            case 38: //UP
                this.frog.startMovement(NORTH);
                break;
            case 87: //W
                this.frog.startMovement(NORTH);
                break;          
            case 39: //Right
                this.frog.startMovement(EAST);
                break;
            case 68: //D
                this.frog.startMovement(EAST);
                break;
            case 40: //Down
                this.frog.startMovement(SOUTH);
                break;
            case 83: //S
                this.frog.startMovement(SOUTH);
                break;
        }
    });

I've also tried to do something like "this.startMovement(EAST);" and the code above is out of a forum.

Thanks for helping me!

Manuel

1 Answer

Manuel Schulze
Manuel Schulze
12,739 Points

I fixed it on my own. It was like:

goog.events.listen(goog.global, ['keydown', 'keyup'], function(e) {
        switch (e.keyCode) {
            case 37: //LEFT
                console.log("Left arrow pressed");
                this.startMovement(WEST);
                break;
...
    }
}, null, this.frog);