css - Device div screens overlapping, how do I fix? -
how change keyboard functions onscreen joystick? building html5 game , im lost how add joystick.
below code needs help. thank in advance :)
function init() { canvas = document.getelementbyid('canvas'); ctx = canvas.getcontext('2d'); enemy = new image(); enemy.src = '8bit_enemy.png'; ship = new image(); ship.src = 'ship.png'; starfield = new image(); starfield.src = 'starfield.jpg'; document.addeventlistener('keydown', keydown, false); document.addeventlistener('keyup', keyup, false); canvas.addeventlistener('click', gamestart, false); gameloop(); } function gamestart() { gamestarted = true; canvas.removeeventlistener('click', gamestart, false); } //the main function of game, calls other functions needed make game run function gameloop() { clearcanvas(); drawstarfield() if (alive && gamestarted && lives > 0) { hittest(); shipcollision(); movelaser(); moveenemies(); drawenemies(); drawship(); drawlaser(); } scoretotal(); game = settimeout(gameloop, 1000 / 30); } //checks see key has been pressed , either move ship or fire laser function keydown(e) { if (e.keycode == 39) rightkey = true; else if (e.keycode == 37) leftkey = true; if (e.keycode == 38) upkey = true; else if (e.keycode == 40) downkey = true; if (e.keycode == 88 && lasers.length <= lasertotal) lasers.push([ship_x + 25, ship_y - 20, 4, 20]); } //checks see if pressed key has been released , stops ships movement if has function keyup(e) { if (e.keycode == 39) rightkey = false; else if (e.keycode == 37) leftkey = false; if (e.keycode == 38) upkey = false; else if (e.keycode == 40) downkey = false; } window.onload = init;
you need this:
function dokeydown(e){ //==================== // w key //==================== if (e.keycode == 87) { clearcanvas(); y = y - 10; canvas_context.fillrect(x, y, 50, 30); } //==================== // s key //==================== if (e.keycode == 83) { clearcanvas(); y = y + 10; canvas_context.fillrect(x, y, 50, 30); } //==================== // key //==================== if (e.keycode == 65) { clearcanvas(); x = x - 10; canvas_context.fillrect(x, y, 50, 30); } //==================== // d key //==================== if (e.keycode == 68) { clearcanvas(); x = x + 10; canvas_context.fillrect(x, y, 50, 30); } } function clearcanvas() { canvas.width = canvas.width; }
the full explanation , code examples can found here , demo here
Comments
Post a Comment