私は非常に基本的なものに完全に行き詰まっています:
コンストラクターを使用して、いくつかのゲーム アイテムを作成しています。
function itemCreator(itemName, itemType, itemPosition) {
this.itemName = itemName;
this.itemType = itemType;
this.itemPosition =itemPosition;
}
new itemCreator('shootUp001', 'item_up', 108 );
new itemCreator('shootLeft001', 'item_left', 608);
new itemCreator('shootLeft002', 'item_left', 40);
後で、次のようなアイテムに画像を割り当てています。
function assignImages(item){
itemObject =item;
itemType = itemObject.itemType;
var itemDiv = document.getElementById(itemType); //get the div that has the name of this item
itemDiv.innerHTML = '<img src="' +itemType +'.png"/><span class="x">x</span><span id="' +itemType +'SpanCount"></span>' //put the picture of this item in there and also a span for the counting
}
これが私が立ち往生している場所です:
特定の itemType の画像を初めて挿入したときに「true」に設定するブール変数を作成するにはどうすればよいですか? 同じタイプの画像を 2 回挿入しないようにするために、これが必要です。
簡単な dom ルックアップを実行できることはわかっていますが、javascript を学習しようとしており、この状況でそれを回避する方法を理解したいと考えています。
itemType に基づいて変数を作成し、一致する itemType を持つオブジェクトが assignImage に渡されたときにその変数を変更するスマートな方法は何でしょうか?