戻る

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

サンプル
  


[CSS]
copy
.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";
}
function judgeGoal()
サイコロを振った数だけ、ひよこを進めています。
しかし、ゴールした場合は、最後の10マス目に停止させてから
メッセージを表示するようにしています。

この処理を実行するため、「setInterval」を使用しています。
setInterval は、指定した間隔(ミリ秒)ごとにコールバック関数を繰り返し実行するタイマーです。
setIntervalはタイマーを識別する正の整数の戻り値(ID)を返します。
※通常は、1から始まる正の整数の値です。
この戻り値(ID)を使ってclearInterval関数を実行すると
戻り値(ID)に一致するタイマーを停止してくれます。
このsetIntervalはclearIntervalを実行するまでタイマーを継続するため
条件文などを使用して処理を手動で停止させます。

let restCount = diceValue;
サイコロの目の数を最大のループ回数と仮定します。
仮定という言葉を使用したのは、ゴールという処理があるからです。
サイコロを振ってもピッタリゴールに止まるサイコロの目が出るわけではありません。
そのため、もしもゴールに到達したらタイマーを中止しますよ
という分岐処理を実装しています。
それが下記のステップです。
if (restCount <= 0 || index >= lastIndex)
(1) restCount <= 0
サイコロの目の数だけ、ひよこを進めたケース

(2) index >= lastIndex
配列の最後のインデックスまで、ひよこを進めたケース

上記(1)もしくは(2)に一致した場合は
clearInterval(intervalId);
を実行してタイマーを止め、終了判定処理の
function judgeEnd(lang)
を実行しています。



[謝辞]
「イラスト:農民イラスト」さんの画像を使用させていただいております。
ありがとうございます。
[URL]イラスト:農民イラスト
URL[https://nawmin.stores.jp][クリックすると開きます])


戻る
back

Move the character at regular intervals
Use a timer to move the character at regular intervals.

sample
  


[CSS]
copy
.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";
}
function judgeGoal()
The chick moves forward the number of times the dice are rolled.
However, if the chick reaches the goal, it stops at the last 10 squares and then displays a message.

To execute this process, we use "setInterval".
setInterval is a timer that repeatedly executes a callback function at a specified interval (milliseconds).
setInterval returns a positive integer return value (ID) that identifies the timer.
*Normally, this is a positive integer value starting from 1.
If you execute the clearInterval function using this return value (ID), it will stop the timer that matches the return value (ID).
This setInterval continues the timer until clearInterval is executed, so
use a conditional statement to manually stop the process.

let restCount = diceValue;
Assume the number of dice is the maximum number of loops.
I use the word "assume" because there is a process called the goal.
Even if you roll the dice, you won't get a number that stops exactly at the goal.
For that reason, we have implemented a branching process that stops the timer if the goal is reached.
These are the steps below.
if (restCount <= 0 || index >= lastIndex)
(1) restCount <= 0
The chicks have been moved forward by the number of dots on the dice.

(2) index >= lastIndex
The chicks have been moved to the last index of the array.

If the above (1) or (2) is met, the timer is stopped by executing
clearInterval(intervalId);
and the end judgment process is executed.
function judgeEnd(lang)



[Acknowledgements]
images used are from "illustration by nawmin.com"
Thank you very much.
[URL]illustration by nawmin.com
URL[https://nawmin.stores.jp][click to open the homepage])


back



著作権情報
ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。
ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、
または公開したりすることはできません。
当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、
その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について
一切の責任を負わないものとします。