我想將游戲從 onload 更改為 onkeydown(空格),但我不知道如何將整個代碼置于 onkeydown 命令下。我是編程愛好者,這是我的第一個項目之一,所以感謝您的幫助:)<body onload="startGame()"><script>function startGame() {myGameArea.start();}var myGameArea = {canvas : document.createElement("canvas"),start : function() { this.canvas.width = 220; this.canvas.height = 400; this.context = this.canvas.getContext("2d"); document.body.insertBefore(this.canvas, document.body.childNodes[0]);this.frameNo = 0;updateGameArea(); },clear : function() { this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);} }function component(width, height, color, x, y, type) {this.type = type;function everyinterval(n) { if ((myGameArea.frameNo / n) % 1 == 0) {return true;} return false;}function accelerate(n) { if (!myGameArea.interval) {myGameArea.interval = setInterval(updateGameArea, 20);} myGamePiece.gravity = n;}function reload(){ window.location.reload(true);}document.onkeydown = function(event){ if(event.keyCode === 27) paused = !paused;}</script><button onmousedown="accelerate(-0.2); myMusic.play();" onmouseup="accelerate(0.05)">szybuj :')</button>jsFiddle:https ://jsfiddle.net/sidka/k1f4ow0q/6/
如何將程序從 onload 開始更改為從 Space press 開始?
慕田峪7331174
2021-06-15 17:52:08