Setting an array position with a variable problem

Hello again

Oh boy, do I have a good one for you today :slight_smile:

I have designed a variable called arrayPos, which defines the first number of a multi-dimensional array, but it doesn’t see to work properly and I am struggling to know why.

It comes at a point in my game where the player can browse budget reports from previous turns. Each budget report is twelve items long which are held in a multi-dimensional array organised by month (turn). So [0][0]-[0][11] holds January, [1][0]-[1][11] holds February and so on.

The function interprets the click of one of twelve ‘month’ buttons the player clicks on, then attempts to work out the first number of the array (the month) from that. Here’s what I have so far



var arrayPos:Number;

//This function determines which month has been clicked from the filing cabinet and creates a variable
//called arrayPos from the information. This is then used as an index for the relevant month's 
//information within the storedBudgetData[] array
function getArrayPosition(monthClicked)
{
    if (monthClicked = folderLabel_mc1)
    {
        targetMonth=0;
    }
    else if (monthClicked = folderLabel_mc2)
    {
        targetMonth=1;
    }
    else if (monthClicked = folderLabel_mc3)
    {
        targetMonth=2;
    }
    else if (monthClicked = folderLabel_mc4)
    {
        targetMonth=3;
    }
    else if (monthClicked = folderLabel_mc5)
    {
        targetMonth=4;
    }
    else if (monthClicked = folderLabel_mc6)
    {
        targetMonth=5;
    }
    else if (monthClicked = folderLabel_mc7)
    {
        targetMonth=6;
    }
    else if (monthClicked = folderLabel_mc8)
    {
        targetMonth=7;
    }
    else if (monthClicked = folderLabel_mc9)
    {
        targetMonth=8;
    }
    else if (monthClicked = folderLabel_mc10)
    {
        targetMonth=9;
    }
    else if (monthClicked = folderLabel_mc11)
    {
        targetMonth=10;
    }
    else if (monthClicked = folderLabel_mc12)
    {
        targetMonth=11;
    }
    arrayPos = yearsPlayed*12 + targetMonth;//yearsPlayed is full years played so is at 0 for the first 12 months.
}

I know, I know… newbie alert. There must have been a faster way using a for loop or something, but I figured there were only ever 12.

I then try to use arrayPos to tell the game which months it should display and to tell it to get on with showing the 12 variables which make up each report. The name of these variables corresponds to their names on the blank budget report.


//This function determines which information from the storedBudgetData[] array corresponds to 
//which variable within historic budget reports. It uses the variable arrayPos defined in 
//getArrayPosition() above

function getHistoricBudgetData()
{
    trace("inside getHistoricBudgetData()");
    trace("arrayPos = "+arrayPos);
    trace("historicBudgetCarriedOver = storedBudgetData[arrayPos][1]. Currently defined as "+storedBudgetData[arrayPos][1]);
    
    historicDate = storedBudgetData[arrayPos][0];
    historicBudgetCarriedOver = storedBudgetData[arrayPos][1];
    historicBudgetThisTurn = storedBudgetData[arrayPos][2];
    historicSciCostThisTurn = storedBudgetData[arrayPos][3];
    historicEngCostThisTurn = storedBudgetData[arrayPos][4];
    historicKeyCostThisTurn = storedBudgetData[arrayPos][5];
    historicNewbuildThisTurn = storedBudgetData[arrayPos][6];
    historicUpgradeCostThisTurn = storedBudgetData[arrayPos][7];
    historicSciWagesThisTurn = storedBudgetData[arrayPos][8];
    historicEngWagesThisTurn = storedBudgetData[arrayPos][9];
    historicKeyWagesThisTurn = storedBudgetData[arrayPos][10];
    historicBudget = storedBudgetData[arrayPos][11];
}

Currently arrayPos is tracing as NaN, so the game seems to treat that like zero, and despite which month is clicked, always displays the historic budget report for January.

Initially all the variables were undefined when I used

var arrayPos = yearsPlayed*12 + targetMonth

but when I declared it seperately, outside the function , as a number, it simply treats arrayPos as a zero.

Can anyone see whaty I am doing wrong? I have been reading up on past answers and trying various combinations, but nothing seems to work.

Many thanks
John

PS. I don’t mind redesigning my code for this, if thats whats called for.