-2

我对 JavaScript 还很陌生,但我是一名资深程序员,我就是不明白发生了什么。

我有这个:

var MainTable = {
    "k1": 0,
    "k2": 9,
    "k3": 20
};

当我想访问特定元素时:

var index = "k1";
var value = MainTable[index];

现在,由于我完全无法理解的原因,它每次都返回 undefined 而不是 0。我已经玩了大约2个小时了,我就是不明白。

谢谢 !

编辑完整代码:

// These shouldn't be modified
var maxEPoints = 29;
var maxGold = 50;
var hudBarWidth_HR = 650; // 650 is for hr (px)
var hudBarWidth_LR = 350;

var IN_COMBAT = false;
var IN_COMBAT_WAITING = false;

var WolfDamageTable = 
{
    "m11_1": 99,
    "m11_2": 99,
    "m11_3": 8,
    "m11_4": 8,
    "m11_5": 7,
    "m11_6": 6,
    "m11_7": 5,
    "m11_8": 4,
    "m11_9": 3,
    "m11_10": 0,
};

var EnemyDamageTable =
{
    "m11_1": 0,
    "m11_2": 0,
    "m11_3": 0,
    "m11_4": 0,
    "m11_5": 1,
    "m11_6": 2,
    "m11_7": 3,
    "m11_8": 4,
    "m11_9": 5,
    "m11_10": 6,
};


function ComputeCombatDamage( aPoints, ePoints, skill, endurance )
{
    var ratio = ( aPoints - parseInt( skill ) ).toString();
    var absRatio = ( aPoints - parseInt( skill ) );
    var randVal = GIMME_A_RANDOM().toString();

    $( "td#tableWolfSkill" ).html( aPoints.toString() );
    $( "td#tableWolfEndurance" ).html( ePoints.toString() );
    $( "td#ratio" ).html( ratio.toString() + " / " + randVal.toString() );

    // ... code where I process the string index

    var eDamage = EnemyDamageTable[eIndex]; // -> problem
    var wDamage = WolfDamageTable[wIndex];

    var wolfEndurance = ePoints - wDamage;
    var enemyEndurance = endurance - eDamage; 

    var barWidth = ( wolfEndurance / maxEPoints ) * hudBarWidth_HR;


    IN_COMBAT = wolfEndurance > 0 && enemyEndurance > 0;

    return IN_COMBAT;
}

/*********************************************************************
 * custom function to initiate the battle sequence
 *********************************************************************/
function LoadBattle()
{
    IN_COMBAT = true;

    var aPoints = 0;
    var ePoints = 0;
    var gold = 0;

    var name = $( "span#enemyName" ).html();
    var skill = $( "span#enemySkill" ).html();
    var endurance = $( "span#enemyEndurance" ).html();

    if( skill.length == 0 ||
        endurance.length == 0 )
    {
        alert( "Content is not properly formated for combat!" );
        return;
    }

    window.location = "#fight_start";
    // Set up the table before showing it
    $( "td#tableEnemyName" ).html( name );
    $( "td#tableEnemySkill" ).html( skill );
    $( "td#tableEnemyEndurance" ).html( endurance );

    var keepGoing = true;

    while( IN_COMBAT )
    {
        if( typeof( Storage ) !== "undefined" )
        {
            aPoints = localStorage.aPoints;
            ePoints = localStorage.ePoints;
        }
        keepGoing = ComputeCombatDamage( aPoints, ePoints, skill, endurance );
        //endurance = $( "td#enemyResultEndurance" ).html();
        if( keepGoing == false )
        {
            break;
        }
        //WaitCombat();
    }
}

对我来说,错误似乎来自

var eDamage = EnemyDamageTable[eIndex];
var wDamage = WolfDamageTable[wIndex];

function ComputeCombatDamage

我现在已经尽量减少了。

4

1 回答 1

1

wIndex并且eIndex没有定义。我可以详细说明,但这应该说明一切。

于 2013-10-20T08:50:16.983 回答