戻る

キャラの場所の変更
A地点からB地点への変更処理

サンプル
  


[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
let index = 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()
{
	//サイコロの目を取得
	let number = dice();
	//サイコロを表示
	displayDice(number);
	index += number;
	//ゴール判定処理
	judgeGoal();
}
//ゴール判定処理
function judgeGoal()
{
	if(index >= 9)
	{
		//最後の位置にします
		index=9;
	}
	//ひよこを移動します。
	moveChick();
	setTimeout(() => 
	{
		if(index < 9)
		{
			return;
		}
		//ボタンを非表示にします。
		const b1 = document.getElementById("btn1");
		b1.style.visibility="hidden";
		//ゴールのメッセージを表示します。
		alert("おめでとうございます。\nゴールしました。");
	}, 0);
}
//サイコロの目を取得
function dice()
{
	const start = 1;
	const end = 6;
	//指定の範囲の整数を返します
	return Math.floor(Math.random() * (end - start + 1)) + start;
}
//サイコロを表示
function displayDice(number)
{
	//サイコロの画像は準備してください
	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;
	moveChick();
	const b1 = document.getElementById("btn1");
	b1.style.visibility="visible";
}

緑色がひよこが通る場所です。
サイコロを振るとサイコロの目の数だけ場所を変更するサンプルです。
リセットボタンを選択すると初期位置に戻ります。
ひよこの通り道のサイズは「.layer2」で設定しています。
intiMap()
この関数で、緑の通り道とひよこの初期位置を設定してます。

diceNumber()
サイコロを振った処理を実行しています。

judgeGoal()
サイコロを振った結果のマス目の分だけ、ひよこを移動させます。
このマス目は、配列の個数でいうと10個あります。
aryXとaryYが緑色のマップ位置であり、ひよこが通る道です。
このサンプルプログラムでの考え方としてインデックスの位置を採用しており、
開始位置がインデックス0
サイコロの目が3だったら
index = 0(現時点のインデックス位置) + 3(サイコロの目);
index = 3;
配列要素3の配列要素の座標にひよこを移動させます。

if(index >= 9)
ゴールはインデックス位置が9ですので、これ以上進ませないことが条件としています。
そのため、サイコロの目を加算した結果により、インデックスの値が9を超えても9に修正をしています。
ゴール判定ですので、ゴールに到達していない場合は、この処理を抜けます。
ゴールに到達している場合は、
(1)サイコロボタンを非表示にする
(2)メッセージを表示する
という処理を実行しています。

mapReset()
リセット処理をします。
ひよこを初期位置に戻します。
ひよこを初期位置に戻すのでインデックス変数もリセットしています。
サイコロボタンを再表示します。

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


戻る
back

Change the character's location
Processing a change from point A to point B

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
let index = 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()
{
	//Get the number on the dice
	let number = dice();
	//Show Dice
	displayDice(number);
	index += number;
	//Goal determination process
	judgeGoal();
}
//Goal determination process
function judgeGoal()
{
	if(index >= 9)
	{
		//Make it the last position
		index=9;
	}
	//Move the chick.
	moveChick();
	setTimeout(() => 
	{
		if(index < 9)
		{
			return;
		}
		//Hide the button.
		const b1 = document.getElementById("btn1");
		b1.style.visibility="hidden";
		//Shows the goal message。
		alert("Congratulations! \nreached the goal.");
	}, 0);
}
//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(number)
{
	//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;
	moveChick();
	const b1 = document.getElementById("btn1");
	b1.style.visibility="visible";
}

The green areas are where the chicks pass.
This is an example where the location changes by the number of dots on the dice when you roll them.
Select the reset button to return to the initial position.
The size of the chicks' path is set in ".layer2".
intiMap()
This function sets the green path and the chicks' initial position.

diceNumber()
The process of rolling the dice is being executed.

judgeGoal()
Move the chick to the number of squares determined by the dice roll.
There are 10 squares in this array.
aryX and aryY are the green map positions, and are the path the chick will take.
The index position is used as the concept in this sample program, and
The starting position is index 0.
If the dice number is 3, then
index = 0 (current index position) + 3 (number on the dice);
index = 3;
Move the chick to the coordinates of the array element of array element 3.

if(index >= 9)
The goal is index position 9, so the condition is not to go any further.
Therefore, even if the index value exceeds 9 due to the addition of the dice number, it is corrected to 9.
Since this is a goal determination, if the goal has not been reached, this process will be terminated.
If the goal has been reached, the following process is executed:
(1) Hide the dice button
(2) Display a message

mapReset()
Performs the reset process.
Returns the chick to its initial position.
The index variable is also reset because the chick is returned to its initial position.
The dice button is displayed again.

[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



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