对于冗长的标题,我很抱歉,但我无法想出更好的描述。 问题在于:我在游戏中定义了一些太空飞船,为此我创建了一个包含所有数据(盾牌等)的简单表格:
var defaultShipDataSheet = [
['Shields', DISPLAY_HANGAR, DISPLAY_HUD, { id: 0, core: 100, base: 100, mod: 100 }],
['Armor', DISPLAY_HANGAR, DISPLAY_HUD, { id: 1, core: 100, base: 100, mod: 100 }],
['Toughness', NO_DISPLAY_HANGAR, NO_DISPLAY_HUD, { id: 2, core: 100, base: 100, mod: 100 }],
['Speed', DISPLAY_HANGAR, DISPLAY_HUD, { id: 3, core: 100, base: 100, mod: 100 }],
['Weight', DISPLAY_HANGAR, DISPLAY_HUD, { id: 4, core: 100, base: 100, mod: 100 }],
['Agility', DISPLAY_HANGAR, NO_DISPLAY_HUD, { id: 5, core: 100, base: 100, mod: 100 }],
['Area Damage', DISPLAY_HANGAR, NO_DISPLAY_HUD, { id: 6, core: 100, base: 100, mod: 100 }],
['Life On Hit', DISPLAY_HANGAR, NO_DISPLAY_HUD, { id: 7, core: 100, base: 100, mod: 100 }],
['Life On Kill', DISPLAY_HANGAR, NO_DISPLAY_HUD, { id: 8, core: 100, base: 100, mod: 100 }],
['Firing Rate', DISPLAY_HANGAR, NO_DISPLAY_HUD, { id: 9, core: 100, base: 100, mod: 100 }],
['Beam Charging Time', DISPLAY_HANGAR, NO_DISPLAY_HUD, { id: 10, core: 100, base: 100, mod: 100 }],
['Bomb Capacity', DISPLAY_HANGAR, DISPLAY_HUD, { id: 11, core: 100, base: 100, mod: 100 }],
['Critical Hit Chance', DISPLAY_HANGAR, NO_DISPLAY_HUD, { id: 12, core: 100, base: 100, mod: 100 }],
['Critical Damage', DISPLAY_HANGAR, NO_DISPLAY_HUD, { id: 13, core: 100, base: 100, mod: 100 }],
['Damage', DISPLAY_HANGAR, NO_DISPLAY_HUD, { id: 14, core: 100, base: 100, mod: 100 }],
];
创建没有标签和显示选项的缩小版本后:
var defaultShipData = {};
for (var m=0; m <= defaultShipDataSheet.length-1; m++) {
propertyName = defaultShipDataSheet[m][0].substr(0, 1).toLowerCase() + defaultShipDataSheet[m][0].substr(1);
propertyName = propertyName.replace(/ /g, "");
defaultShipData[propertyName] = defaultShipDataSheet[m][3];
}
然后我宣布让我们说几艘船:
var allShips = [
{ id: 0,
name: 'Sparrow',
country: 'US',
code: 'LT-21',
shipData: defaultShipData,
spriteSheet: 'lt21.json',
weaponGrid: defaultWeaponGrid },
{ id: 1,
name: 'Industrial Posterites',
country: 'US',
code: 'INPO-715',
shipData : defaultShipData,
spriteSheet: 'lt21.json',
weaponGrid: defaultWeaponGrid }]
然后我覆盖定制船的值:
/* Industrial Posterites */
allShips[1].shipData.shields.core = 20;
然后将其分配给整个范围的船舶(以及相关的defaultShipData对象)。 这是对defaultShipData对象的捕获:
我想这是因为它全部通过引用传递,但是我该如何避免这种情况?
我看过克隆方法,但我不确定它是否是正确的方法。