戻る

キャラ移動による画面切り替え
移動による画面を切り替える例

サンプル
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26


ひよこが移動することにより、マップを切り替えるサンプルです。
[HTML]
[CSS]
copy
.container{
	position: sticky;
}
.layer{
	width: 250px;
	height: 250px;
	position: absolute;
}
.layer1{
	top: 0px;
	left: 0px;
	z-index:1;
}
.layer2{
	top: 0px;
	left: 0px;
}
.layer3{
	top: 250px;
	left: 0px;
	width: 100px;
	height: 30px;
	z-index:2;
}
.layer4{
	top: 250px;
	left: 150px;
	width: 100px;
	height: 30px;
	z-index:2;
}
[javascript]
copy
//マップ座標
const aryX=[200,200,200,200,200,150,100, 50,  0,   200,150,100, 50,  0,  0,  0,  0, 50,100,150,200,200,     0, 50,100,100,100];
const aryY=[0  , 50,100,150,200,200,200,200,200,   200,200,200,200,200,150,100, 50, 50, 50, 50, 50,100,   100,100,100, 50,  0];
let index = 0;
let x=0;
let y=0;
mapA();
//ひよこの位置
chickPosition();
//map A
function mapA()
{
	hiddenAllMap();
	displayMap(0,8);
	displayMap(22,26);
}
//map B
function mapB()
{
	hiddenAllMap();
	displayMap(9,21);
}
//ひよこの位置
function chickPosition()
{
	const chick = document.getElementById("chick");
	chick.style.left = aryX[index] +'px';
	chick.style.top = aryY[index] +'px';
}
//マップ表示
function displayMap(startIndex,endIndex)
{
	for(i=startIndex; i <= endIndex; i++)
	{
		const map = document.getElementById("map"+i);
		map.style.visibility = "visible";
		map.style.left = aryX[i] +'px';
		map.style.top = aryY[i] +'px';
	}
}
//マップ非表示
function hiddenAllMap()
{
	for(i=0; i < aryX.length; i++)
	{
		const map = document.getElementById("map"+i);
		map.style.visibility = "hidden";
	}
}
const b1 = document.getElementById("b1");
b1.addEventListener("click", () => 
{
	moveChick(1);
});
const b2 = document.getElementById("b2");
b2.addEventListener("click", () => 
{
	moveChick(-1);
});
//ひよこ移動
function moveChick(direction)
{
	index += direction;
	if(index >= aryX.length)
	{
		index = aryX.length - 1;
	}
	if(index<0)
	{
		index =0;
	}
	switch(index)
	{
		case 8:
		case 22:
			mapA();
			break;
		case 9:
		case 21:
			mapB();
			break;
	}
	chickPosition();
}
[画面切り替え処理]
switch(index)
{
	case 8:
	case 22:
		mapA();
		break;
	case 9:
	case 21:
		mapB();
		break;
}
例えば
マップAの画面のマス目「8」から「9」に切り替わるとき
画面がマップBに切り替わる処理を入れています。
マップBの画面のマス目「21」から「22」に切り替わるとき
画面がマップAに切り替わる処理を入れています。

マップの表示を一度クリアしてから
指定したマップの情報のみ表示しています。
[マップの表示を一度クリア]
function hiddenAllMap()
{
	for(i=0; i < aryX.length; i++)
	{
		const map = document.getElementById("map"+i);
		map.style.visibility = "hidden";
	}
}
map.style.visibility = "hidden";
「style.visibility」に「hidden」を設定することでマップのdivタグを非表示にしています。

[指定したマップの情報を表示]
function displayMap(startIndex,endIndex)
{
	for(i=startIndex; i <= endIndex; i++)
	{
		const map = document.getElementById("map"+i);
		map.style.visibility = "visible";
		map.style.left = aryX[i] +'px';
		map.style.top = aryY[i] +'px';
	}
}
map.style.visibility = "visible";
「style.visibility」に「visible」を設定することでマップのdivタグを表示状態にします。

map.style.left = aryX[i] +'px';
map.style.top = aryY[i] +'px';
divタグを座標を指定して移動することで表示します。


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


戻る
back

Screen switching by moving the character
Example of switching screens by moving

sample
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26


This is an example of switching maps by moving the chick.
[HTML]
[CSS]
copy
.container{
	position: sticky;
}
.layer{
	width: 250px;
	height: 250px;
	position: absolute;
}
.layer1{
	top: 0px;
	left: 0px;
	z-index:1;
}
.layer2{
	top: 0px;
	left: 0px;
}
.layer3{
	top: 250px;
	left: 0px;
	width: 100px;
	height: 30px;
	z-index:2;
}
.layer4{
	top: 250px;
	left: 150px;
	width: 100px;
	height: 30px;
	z-index:2;
}
[javascript]
copy
//Map Coordinates
const aryX=[200,200,200,200,200,150,100, 50,  0,   200,150,100, 50,  0,  0,  0,  0, 50,100,150,200,200,     0, 50,100,100,100];
const aryY=[0  , 50,100,150,200,200,200,200,200,   200,200,200,200,200,150,100, 50, 50, 50, 50, 50,100,   100,100,100, 50,  0];
let index = 0;
let x=0;
let y=0;
mapA();
//Chick position
chickPosition();
//map A
function mapA()
{
	hiddenAllMap();
	displayMap(0,8);
	displayMap(22,26);
}
//map B
function mapB()
{
	hiddenAllMap();
	displayMap(9,21);
}
//Chick position
function chickPosition()
{
	const chick = document.getElementById("chick");
	chick.style.left = aryX[index] +'px';
	chick.style.top = aryY[index] +'px';
}
//Map display
function displayMap(startIndex,endIndex)
{
	for(i=startIndex; i <= endIndex; i++)
	{
		const map = document.getElementById("map"+i);
		map.style.visibility = "visible";
		map.style.left = aryX[i] +'px';
		map.style.top = aryY[i] +'px';
	}
}
//Map hidden
function hiddenAllMap()
{
	for(i=0; i < aryX.length; i++)
	{
		const map = document.getElementById("map"+i);
		map.style.visibility = "hidden";
	}
}
const b1 = document.getElementById("b1");
b1.addEventListener("click", () => 
{
	moveChick(1);
});
const b2 = document.getElementById("b2");
b2.addEventListener("click", () => 
{
	moveChick(-1);
});
//Chick moving
function moveChick(direction)
{
	index += direction;
	if(index >= aryX.length)
	{
		index = aryX.length - 1;
	}
	if(index<0)
	{
		index =0;
	}
	switch(index)
	{
		case 8:
		case 22:
			mapA();
			break;
		case 9:
		case 21:
			mapB();
			break;
	}
	chickPosition();
}
[Screen switching process]
switch(index)
{
	case 8:
	case 22:
		mapA();
		break;
	case 9:
	case 21:
		mapB();
		break;
}
For example,
When the square on the Map A screen changes from "8" to "9",
the screen switches to Map B.
When the square on the Map B screen changes from "21" to "22",
the screen switches to Map A.

The map display is cleared once,
and only the information of the specified map is displayed.
[Clear the map display once]
function hiddenAllMap()
{
	for(i=0; i < aryX.length; i++)
	{
		const map = document.getElementById("map"+i);
		map.style.visibility = "hidden";
	}
}
map.style.visibility = "hidden";
The map div tag is hidden by setting "style.visibility" to "hidden".

[Show the specified map information]
function displayMap(startIndex,endIndex)
{
	for(i=startIndex; i <= endIndex; i++)
	{
		const map = document.getElementById("map"+i);
		map.style.visibility = "visible";
		map.style.left = aryX[i] +'px';
		map.style.top = aryY[i] +'px';
	}
}
map.style.visibility = "visible";
By setting "style.visibility" to "visible", the map div tag will be displayed.

map.style.left = aryX[i] +'px';
map.style.top = aryY[i] +'px';
The div tag is displayed by specifying the coordinates and moving it.


[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



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