###前提・実現したいこと
ゲームを作りながら楽しく学べるHTML5+CSS+JavaScriptプログラミングを見ながらゲームを作っているんですが、左右キーを押しても動きません。
###該当のソースコード
html
1<!DOCTYPE html> 2<html> 3<head> 4 5<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/> 6 <style> 7 div { 8 position:absolute; 9 width:60px; 10 height:25px; 11 color:green; 12 top: 50px; 13 } 14 </style> 15 16 <script> 17 var ufo; 18function init() { 19 window.addEventListener("keydown", keydown); 20 ufo = new UFO(document.getElementById("ufo"), 100); 21} 22 23function keydown(e) { 24 if (e.keycode == 37) { 25 ufo.moveLeft(); 26 } 27 else if (e.keycode == 39) { 28 ufo.moveRight(); 29 } 30} 31 32function UFO(_elem,_xpos){ 33 this.elem = _elem; 34 this.xpos = _xpos; 35 this.moveLeft = function () { 36 this.xpos -= 10; 37 this.elem.style.left = this.xpos + "px"; 38 } 39 this.moveRight = function () { 40 this.xpos += 10; 41 this.elem.style.left = this.xpos + "px"; 42 } 43} 44 45 </script> 46 <title>UFOゲーム</title> 47 <meta charset="utf-8" /> 48</head> 49<body onload="init()"> 50 <div id="ufo"><(ooo)></div> 51</body> 52</html> 53
###補足情報(言語/FW/ツール等のバージョンなど)
より詳細な情報
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。