我正在製作一個遊戲,玩家可以選擇3個不同的角色。然而,我遇到了一個明顯的問題,那就是當我創建一個函數(如攻擊函數)時,它只鏈接到一個特定的字符。Javascript:你如何輸入未知變量?
我寧願寫我的代碼,當人選擇他們的角色時,所有人都可以使用相同的攻擊技能,而不必編寫3個不同的角色。另外,攻擊技能與一個按鈕相關聯,所以它必須是多樣的。
我不能有一個指定 X播放器的攻擊按鈕。那麼,如何製作我的代碼,以便它可以添加所有字符而不是僅指定一個字符?
例如:看我的功能下面爲罷工攻擊。我可以把它設置爲侏儒&天使這很好。然而,如果玩家選擇ELF字符呢?那麼這個功能將不起作用,因爲它認爲角色是一個矮人,與天使戰鬥。我怎樣才能解決這個問題?
New=Object.create;
actor = {
primaryStats: function (level, hp, hpcap, balance, balancecap, exp){
this.level = level;
this.hp = hp;
this.hpcap = hpcap;
this.balance = balance;
this.balancecap = balancecap;
this.exp = exp;
},
player = New (actor),
monster = New (actor),
dwarf = New(player),
human = New(player),
elf = New(player),
angel = New(monster),
demon = New(monster),
dragon = New(monster);
//ATTACK SKILL ONE
dom.el("strike").onclick = function strike() {
playerHitCalc(dwarf, angel);
};
playerHitCalc = function(character, boss){
roll = Math.floor(Math.random() * character.accuracy + 1);
if (roll > boss.agility){
var hit = true;
}
else {
hit = false;
logMessage(boss.ID + " " + "has evaded your attack!")
}
playerDamCalc = function(){
if (hit == true){ //If you score a successful hit
var damage = Math.floor(Math.random() * character.strength + 1);
var totalDamage = damage - boss.armor; // Subtract Damage from Bosses Armor
if(totalDamage <= 0)totalDamage += boss.armor; // Add boss armor to prevent Negative Numbers
boss.hp -= totalDamage; // Subtract bosses HP from damage.
character.exp += totalDamage * 0.25; // Gain 1 exp point per 4 damage done
dom.setText("bosshealthcounter", boss.hp) // Update Bosses Health
logMessage("You hit " + boss.ID + " for " + totalDamage + " damage!");
}
http://stackoverflow.com/questions/892595/understanding-原型繼承式的JavaScript – kay