kittykatattack / hexi

Make games the fun way!
MIT License
555 stars 83 forks source link

Hexi keyboard movementJS #11

Closed lawalm closed 8 years ago

lawalm commented 8 years ago

Hello, The example is producing this error. movement.js:102 Uncaught TypeError: Cannot read property 'isDown' of undefineddownArrow.release @ movement.js:102key.upHandler @ core.js:153;

I have it does work without the 'isDown'. What am I missing?

See code below, let g = hexi(512, 512, setup, ["assets/crate.png"]); g.start(); g.scaleToWindow();

let crate, message; function setup() {

crate = g.sprite("assets/crate.png"); g.stage.putCenter(crate);

message = g.text( "Use the arrow keys to move the crate", "18px Futura", "black", 6, 6 );

g.state = play; } function play() { crate.x += crate.vx; crate.y += crate.vy;

g.contain(crate, g.stage); } //Alternatively, create some keyboard objects using Hexi's keyboard method. //You would usually use this code in the setup function let leftArrow = g.keyboard(37), upArrow = g.keyboard(38), rightArrow = g.keyboard(39), downArrow = g.keyboard(40);

//Here's how to customize the press and release methods of
//these new arrow keys:

//Assign key press methods leftArrow.press = () => { //Change the crate's velocity when the key is pressed crate.vx = -5; crate.vy = 0; }; leftArrow.release = () => { //If the left arrow has been released, and the right arrow isn't down, //and the crate isn't moving vertically: //Stop the crate if (!g.rightArrow.isDown && crate.vy === 0) { crate.vx = 0; } }; upArrow.press = () => { crate.vy = -5; crate.vx = 0; }; upArrow.release = () => { if (!g.downArrow.isDown && crate.vx === 0) { crate.vy = 0; } }; rightArrow.press = () => { crate.vx = 5; crate.vy = 0; }; rightArrow.release = () => { if (!g.leftArrow.isDown && crate.vy === 0) { crate.vx = 0; } }; downArrow.press = () => { crate.vy = 5; crate.vx = 0; }; downArrow.release = () => { if (!g.upArrow.isDown && crate.vx === 0) { crate.vy = 0; } };

Regards Marlene.

kittykatattack commented 8 years ago

Thanks Marlene, you found a bug in that example code! 😄 Just remove the g. from upArrow, downArrow, rightArrow and leftArrow and it should work.

I've fixed this in the latest version of Hexi, check out the updated keyboardMovement example here:

https://github.com/kittykatattack/hexi/blob/master/examples/src/keyboardMovement.js

Thanks for spotting that!