前提・実現したいこと
現在、ゲーム制作を通してjavascriptの勉強をしています。
しかし、知識不足で図形を移動するための前処理である画面のリセットがうまくいかないので、処理方法を教えていただきたいです。
発生している問題・エラーメッセージ
画面のリセットがうまくいかず、画面が真っ黒でその上から描画したい円が描画できていません。
該当のソースコード
JavaScript
1//キャンバスサイズ 2const SCREEN_W = 1200; 3const SCREEN_H = 500; 4 5const STARS_NUM = 200; //星の数 6const GAME_SPEED = 100; //ゲーム進行のスピード 7 8let can = document.getElementById("can"); 9let con = can.getContext("2d"); 10can.width = SCREEN_W; 11can.height = SCREEN_H; 12can.style.border = "4px solid #555"; 13 14//背景 15con.fillStyle = "black"; 16con.fillRect(0, 0, SCREEN_W, SCREEN_H); 17 18con.beginPath(); 19con.fillStyle = 'white'; 20con.fill(); 21con.closePath(); 22 23let stars = []; //星の座標を格納 24 25setStars(); 26drawStars(); 27setInterval(moveStars, GAME_SPEED); 28 29//星の移動 30function moveStars(){ 31 clear(); 32 for(let i=0; i<stars.length; i++){ 33 let star = stars[i]; 34 let x = star[0]; 35 if(x < 0) 36 stars[i][0] = SCREEN_W; 37 else 38 stars[i][0] = x - 1; 39 } 40 drawStars(); 41} 42 43//画面をクリア 44function clear(){ 45 con.clearRect(0, 0, SCREEN_W, SCREEN_H); 46 con.beginPath(); 47 con.fillStyle = "black"; 48 con.fillRect(0, 0, SCREEN_W, SCREEN_H); 49 con.closePath(); 50} 51 52 53//星の座標を決める 54function setStars() { 55 for(let i=0; i<STARS_NUM; i++){ 56 let x = Math.floor(Math.random()*SCREEN_W); 57 let y = Math.floor(Math.random()*SCREEN_H); 58 stars[i] = [x,y]; 59 } 60} 61 62//星の描画 63function drawStars() { 64 for(let i=0; i<stars.length; i++){ 65 let star = stars[i] 66 let x = star[0]; 67 let y = star[1]; 68 con.beginPath(); 69 con.filleStyle = "white"; 70 con.arc(x, y, 1, 0, 2 * Math.PI, false); 71 con.fill(); 72 con.closePath(); 73 } 74}
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。