それぞれ名前と画像を含む MapIcon でいっぱいの配列があります。
配列は mapIcon と呼ばれます。
各インスタンスを個別に定義するときに機能するこれら 3 つの関数がありますが、ユーザーがどの画像をクリックしたかを判断する 1 つの関数にしようとすると、問題が発生します。
mapIcon[0].click(function(){
var selectedMap = ($('<img src="img/' + maps[0].image + '">'));
gameplayScreen.append(selectedMap);
mapSelectScreen.fadeOut(500, function() {
gameplayScreen.fadeIn(500);
gameplayScreen.show();
});
});
mapIcon[1].click(function(){
var selectedMap = ($('<img src="img/' + maps[1].image + '">'));
gameplayScreen.append(selectedMap);
mapSelectScreen.fadeOut(500, function() {
gameplayScreen.fadeIn(500);
gameplayScreen.show();
});
});
mapIcon[2].click(function(){
var selectedMap = ($('<img src="img/' + maps[2].image + '">'));
gameplayScreen.append(selectedMap);
mapSelectScreen.fadeOut(500, function() {
gameplayScreen.fadeIn(500);
gameplayScreen.show();
});
});
gameplayScreen.click(function() {
gameplayScreen.fadeOut(500, function() {
mapSelectScreen.fadeIn(500);
mapSelectScreen.show();
gameplayScreen.empty();
});
});
マップをクリックすると、gameplayScreen に移動し、もう一度クリックすると、これらの機能のように元に戻ります。
forループを使用し、クリックのためにそれを反復することに関係があると思いますが、mapIcon[i].click(function(){/*foo*/})
動作させることができませんでした。
の配列要素を取得するのは好きではないと思い.click
ます。何か案は?これは、オールインワン機能の現在の試みです。
for (i=0; i< mapIcon.length; i++){
mapIcon[i].click(function(){
var selectedMap = ($('<img src="img/' + mapIcon[i].image + '">'));
gameplayScreen.append(selectedMap);
mapSelectScreen.fadeOut(500, function() {
gameplayScreen.fadeIn(500);
gameplayScreen.show();
console.log(mapIcon[i].name);
});
});
}