Can't achieve jumping in Javascript Game -
i developing javascript game (almost based on tutorial yet, not worried of sharing code).
the problem is, can't character jump after pressing space button. please, can @ code , me?
// edit: sorry lack of information provided. thing - code written, game in state, character animated (=is running) , backgrounds moving. yesterday, tried implement basic controls, such jump pressing spacebar. thing is, player won't jump @ all, , browser console not giving me error statements.
character defined player on line 5. , 321. in code provided below.
the jumping defined in following examples:
pressing space button
var key_codes = { 32: 'space' }; var key_status = {}; (var code in key_codes) { if (key_codes.hasownproperty(code)) { key_status[key_codes[code]] = false; } } document.onkeydown = function(e) { var keycode = (e.keycode) ? e.keycode : e.charcode; if (key_codes[keycode]) { e.preventdefault(); key_status[key_codes[keycode]] = true; } }; document.onkeyup = function(e) { var keycode = (e.keycode) ? e.keycode : e.charcode; if (key_codes[keycode]) { e.preventdefault(); key_status[key_codes[keycode]] = false; } };
other jump information (please, read comments in code)
this.update = function() { // jump, if characted not jumping or falling if (key_status.space && this.dy === 0 && !this.isjumping) { this.isjumping = true; this.dy = this.jumpdy; jumpcounter = 12; assetloader.sounds.jump.play(); } // longer jump if space bar pressed down longer if (key_status.space && jumpcounter) { this.dy = this.jumpdy; } jumpcounter = math.max(jumpcounter-1, 0); this.advance(); // gravity if (this.isfalling || this.isjumping) { this.dy += this.gravity; } // change animation is-falling if (this.dy > 0) { this.anim = this.fallanim; } // change animation is-jumping else if (this.dy < 0) { this.anim = this.jumpanim; } else { this.anim = this.walkanim; } this.anim.update(); }; /** * update sprite's position player's speed */ this.update = function() { this.dx = -player.speed; this.advance(); }; /** * draw current player's frame */ this.draw = function() { this.anim.draw(this.x, this.y); }; } player.prototype = object.create(vector.prototype);
everything seems fine me, player won't move. :(
any help?
if curious full code, go here: http://pastebin.com/dhzkhbmt
edit2:
thank replies far.
i have moved requestanimframe
end of function - keep in mind, thanks.
i have implemented simple jumping script ashish provided above, character still not jumping.
this looks now:
/** jump keys definition **/ $(document).keypress(function(e){ if(e.which==32){ $('player.prototype').css({'top':"0px"}); } settimeout(function(){ $('player.prototype').css({'top':"200px"}); },350); }); /** defining character **/ function player(x, y) { this.dy = 0; this.gravity = 1; this.speed = 6; this.jumpdy = -10; this.isjumping = false; this.width = 60; this.height = 96; this.sheet = new spritesheet('imgs/normal_walk.png', this.width, this.height); this.walkanim = new animation(this.sheet, 4, 0, 11); this.jumpanim = new animation(this.sheet, 4, 3, 3); this.fallanim = new animation(this.sheet, 4, 3, 3); this.anim = this.walkanim; vector.call(this, x, y, 0, this.dy); var jumpcounter = 0; // maximalna dlzka drzania tlacidla skakania } player.prototype = object.create(vector.prototype);
where wrong?
i've tried in http://jsfiddle.net/ykge9/1/
and have infinite loop in animate, requestanimframe should @ end of function:
/** * loop cykly hry */ function animate() { background.draw(); (i = 0; < ground.length; i++) { ground[i].x -= player.speed; ctx.drawimage(assetloader.imgs.grass, ground[i].x, ground[i].y+250); } if (ground[0].x <= -platformwidth) { ground.shift(); ground.push({'x': ground[ground.length-1].x + platformwidth, 'y': platformheight}); } player.anim.update(); player.anim.draw(64, 260); requestanimframe( animate ); }
Comments
Post a Comment