| キャラを一定時間ごとに動かす |
| タイマー処理を使用して一定時間ごとにキャラを移動させます |

.container{
position: sticky;
}
.layer{
width: 200px;
height: 200px;
position: absolute;
}
.layer1{
top: 0px;
left: 150px;
z-index:1;
}
.layer2{
width: 50px;
height: 50px;
}
copy
const lastIndex = 9;//最後のマス目のインデックス
let index = 0;
//ひよこの歩く判定のため追加
let flgStop = 1;//[0]動作可能 [1]停止状態
let diceValue = 0;
const aryX=[150,150,150,150,100,50,0,0,0,0];
const aryY=[0,50,100,150,150,150,150,100,50,0];
intiMap();
//マップの通り道を設定
function intiMap()
{
//通り道
for(i=0; i < aryX.length; i++)
{
const obj = document.getElementById("map"+i);
obj.style.left = aryX[i] +'px';
obj.style.top = aryY[i] +'px';
}
//ひよこを初期位置にします
moveChick()
}
const b1 = document.getElementById("btn1");
b1.addEventListener("click", () =>
{
//サイコロを振る
diceNumber();
});
const b2 = document.getElementById("btn2");
b2.addEventListener("click", () =>
{
//リセット
mapReset();
});
//サイコロを振る
function diceNumber()
{
if(flgStop == 0) return;
//サイコロの目を取得
diceValue = dice();
//サイコロを表示
displayDice();
//ゴール判定処理
judgeGoal();
}
//ゴール判定処理
function judgeGoal()
{
flgStop = 0;//動作可能
let restCount = diceValue;
const intervalId = setInterval(() =>
{
// 止める条件
if (restCount <= 0 || index >= lastIndex)
{
clearInterval(intervalId);
flgStop = 1;
judgeEnd();
return;
}
index++;
moveChick();
restCount--;
}, 1000);
}
function judgeEnd()
{
flgStop = 1;
if(index < lastIndex)
{
return;
}
//ボタンを非表示にします。
const b1 = document.getElementById("btn1");
b1.style.visibility="hidden";
//ゴールのメッセージを表示します。
alert("おめでとうございます。\nゴールしました。");
}
//サイコロの目を取得
function dice()
{
const start = 1;
const end = 6;
//指定の範囲の整数を返します
return Math.floor(Math.random() * (end - start + 1)) + start;
}
//サイコロを表示
function displayDice()
{
//サイコロの画像は準備してください
let file = "";
switch(number)
{
case 1:
file = "dice1.png";
break;
case 2:
file = "dice2.png";
break;
case 3:
file = "dice3.png";
break;
case 4:
file = "dice4.png";
break;
case 5:
file = "dice5.png";
break;
case 6:
file = "dice6.png";
break;
}
const img = document.getElementById("dice");
img.src = img;
}
//ひよこの場所変更
function moveChick()
{
//ひよこ
const obj = document.getElementById("hiyoko");
obj.style.left = aryX[index] +'px';
obj.style.top = aryY[index] +'px';
}
//リセット
function mapReset()
{
index = 0;
//ひよこの歩く判定のため追加
flgStop = 1;//[0]動作可能 [1]停止状態
diceValue = 0;
moveChick();
//サイコロを振るボタンを見える状態にします
const b1 = document.getElementById("btn1");
b1.style.visibility = "visible";
}
| [URL]イラスト:農民イラスト | |
| URL[https://nawmin.stores.jp][クリックすると開きます]) |
| Move the character at regular intervals |
| Use a timer to move the character at regular intervals. |

.container{
position: sticky;
}
.layer{
width: 200px;
height: 200px;
position: absolute;
}
.layer1{
top: 0px;
left: 150px;
z-index:1;
}
.layer2{
width: 50px;
height: 50px;
}
copy
const lastIndex = 9;//Index of the last square
let index = 0;
//Added for chick walking judgment
let flgStop = 1;//[0] Operable [1] Stopped
let diceValue = 0;
const aryX=[150,150,150,150,100,50,0,0,0,0];
const aryY=[0,50,100,150,150,150,150,100,50,0];
intiMap();
//Set the map route
function intiMap()
{
//path
for(i=0; i < aryX.length; i++)
{
const obj = document.getElementById("map"+i);
obj.style.left = aryX[i] +'px';
obj.style.top = aryY[i] +'px';
}
//Set the chick to its initial position
moveChick()
}
const b1 = document.getElementById("btn1");
b1.addEventListener("click", () =>
{
//Roll the dice
diceNumber();
});
const b2 = document.getElementById("btn2");
b2.addEventListener("click", () =>
{
//Reset
mapReset();
});
//Roll the dice
function diceNumber()
{
if(flgStop == 0) return;
//Get the number on the dice
diceValue = dice();
//Show Dice
displayDice();
//Goal determination process
judgeGoal();
}
//Goal determination process
function judgeGoal()
{
flgStop = 0;//operational
let restCount = diceValue;
const intervalId = setInterval(() =>
{
// Stop conditions
if (restCount <= 0 || index >= lastIndex)
{
clearInterval(intervalId);
flgStop = 1;
judgeEnd();
return;
}
index++;
moveChick();
restCount--;
}, 1000);
}
function judgeEnd()
{
flgStop = 1;
if(index < lastIndex)
{
return;
}
//Hide the button.
const b1 = document.getElementById("btn1");
b1.style.visibility="hidden";
//Displays the goal message.
alert("Congratulations! \nreached the goal.");
}
//Get the number on the dice
function dice()
{
const start = 1;
const end = 6;
//Returns an integer in the specified range
return Math.floor(Math.random() * (end - start + 1)) + start;
}
//Show Dice
function displayDice()
{
//Please prepare a picture of the dice
let file = "";
switch(number)
{
case 1:
file = "dice1.png";
break;
case 2:
file = "dice2.png";
break;
case 3:
file = "dice3.png";
break;
case 4:
file = "dice4.png";
break;
case 5:
file = "dice5.png";
break;
case 6:
file = "dice6.png";
break;
}
const img = document.getElementById("dice");
img.src = img;
}
//Change chick location
function moveChick()
{
//chick
const obj = document.getElementById("hiyoko");
obj.style.left = aryX[index] +'px';
obj.style.top = aryY[index] +'px';
}
//Reset
function mapReset()
{
index = 0;
//Added for chick walking judgment
flgStop = 1;//[0] Operable [1] Stopped
diceValue = 0;
moveChick();
//Make the dice roll button visible
const b1 = document.getElementById("btn1");
b1.style.visibility = "visible";
}
| [URL]illustration by nawmin.com | |
| URL[https://nawmin.stores.jp][click to open the homepage]) |
| ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。 ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、 または公開したりすることはできません。 当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、 その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について 一切の責任を負わないものとします。 |