javascript - Make gravity in HTML5 canvas work twice -
this jsfiddle: http://jsfiddle.net/au6br/13/
the problem when keep pressing keydown.up player jump multiple times. want define variable called jumpcount when player jump if jump variable greater value jumping stop.
character.prototype.jump = function () { // jumping character.maxjump += gravity; character.y += character.maxjump; if (keydown.up) { if (!character.jumping) { character.jumping = true; character.pos = 4; character.row = 2; character.h = 23; character.maxjump = -character.sp * 2.5; } } if (character.y >= ch - character.h) { // if character under bottom character.y = ch - character.h; character.jumping = false; } };
the first problem can jump when you're not on ground. it's because of onground function.
replace :
return this.y <= ch - this.h; with :
return this.y >= ch - this.h; you should use function in jump avoid duplicate code :
if (character.onground()) { // if character under bottom character.y = ch - character.h; character.jumping = false; } counting jumps (i assume want make double jumps) can't work long test if(keydown.up). when press key, value true more 1 frame (depending on duration player presses it), you'll never able correctly count number of jumps. must bind jump on onkeydown event, called once. after that, simple replace character.jumping integer.
Comments
Post a Comment