kittykatattack / hexi

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

Using spacebar for jump - help needed #13

Open lawalm opened 7 years ago

lawalm commented 7 years ago

Hello, The sprite object is not responding to 'spaceBar' being pressed(the sprite doesn't jump). See code below, I can't figure out the coding error.

function setup() { dungeon = g.sprite('assets/dungeon.png');

//platform

platform = g.sprite('assets/platform.png'); platform.x = 25; platform.y = 330;

cat = g.sprite('assets/cat.png'); cat.accelerationX = 0; cat.accelerationY = 0; cat.frictionX = 1; cat.frictionY = 1; cat.gravity = 0.3; cat.jumpForce = -6.8; cat.vx = 0; cat.vy = 0; cat.isOnGround = true;

let leftArrow = g.keyboard(37), rightArrow = g.keyboard(39), spaceBar = g.keyboard(32);

//Left arrow key leftArrow.press = () => { if (rightArrow.isUp) { cat.accelerationX = -0.2; } }; leftArrow.release = () => { if (rightArrow.isUp) { cat.accelerationX = 0; } }; //Right arrow key rightArrow.press = () => { if (leftArrow.isUp) { cat.accelerationX = 0.2; } }; rightArrow.release = () => { if (leftArrow.isUp) { cat.accelerationX = 0; } }; //Space key (jump) spaceBar.press = () => { console.log('spacebar pressed') // if (cat.isOnGround) { cat.vy += cat.jumpForce; cat.isOnGround = false; cat.frictionX = 1; } //};

//Change the state to play g.state = play;
} function play() { if(cat.isOnGround) { cat.frictionX = 0.92; } else { cat.frictionY = 0.97; }

//apply acceleration
cat.vx += cat.accelerationX;
cat.vy += cat.accelerationY;

//friction
cat. vx *= cat.frictionX;

//gravity
cat.vy += cat.gravity;

//

// cat.jumpForce = -0.5;

g.move(cat);

/*
g.contain(cat, {
    x: 32, y: 16,
    width: g.canvas.width - 32,
    height:g.canvas.height - 32
});
*/
g.contain(cat, g.stage, false);   

}

Regards and thanks marlene. p.s Will put folder in dropbox.

kittykatattack commented 7 years ago

Is the console.log message "spacebar pressed" displaying?

lawalm commented 7 years ago

Yes, it does. Thanks for your assistance.

On Wed, Jul 13, 2016 at 5:43 PM, kittykatattack notifications@github.com wrote:

Is the console.log message "spacebar pressed" displaying?

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/kittykatattack/hexi/issues/13#issuecomment-232414397, or mute the thread https://github.com/notifications/unsubscribe/ALP51n0titwFaeVpYEk_ptf4setufYmmks5qVRWpgaJpZM4JLPk4 .