Counting in Loop

Here’s a dumb question: What’s the best way to add up numbers in a loop?

I have four textfields whose heights are:


17
17
33
50

And I want to add these up inside a loop - something like the code below (which doesn’t actually work but you get the idea)


    for (var i = 0; i < len; i++) {
        var addHeights:Number = 0;
        addHeights += mc.choiceTxt.textHeight;
    }
var bttnPosition:Number = addHeights + 30;

var addHeights:Number = 0;
var txtArray:Array = [mc.txt1, mc.txt2, mc.txt3];
for (var i:Number = 0; i < txtArray.length; i++) {        
        addHeights += txtArray*.textHeight;
}
var bttnPosition:Number = addHeights + 30;

Something like that.

Thanks - I should show you the full code loop (this all works except for calculating the button position):


    for (var i = 0; i < len; i++) {
        var mc = this.path.question_mc.attachMovie("choice_mc", "choice" + i + "_mc", i);
        mc.choiceTxt.htmlText = this.questionArr[this.currentQues].choiceArr*;
        //mc.choiceTxt.autoSize = "left"; // "left" interferes with calculations for button placement
        mc.choiceTxt.autoSize = "true";
        mc.indexNum = i;
        mc._y = yPosition;
        yPosition = yPosition + mc.choiceTxt.textHeight + this.choiceBuffer;
    }

Given what you’ve shown me I think I need to code something like:


        addHeights += this.path.question_mc.choice*_mc.textHeight;

ButI know that access operator usage must be wrong.

I’ve tried using

addHeights += this.path.question_mc["choice" + i + "_mc"].choiceTxt.textHeight;

but when I trace the addHeights var I get “NaN”.

hi there

well i’ll be honnest. cant figure what your problem is, but i hope this helps


var yPosition:Number = 0
for (var i = 0; i < len; i++) {
        var mc = this.path.question_mc.attachMovie("choice_mc", "choice" + i + "_mc", i);
        mc.choiceTxt.htmlText = this.questionArr[this.currentQues].choiceArr*;
        //mc.choiceTxt.autoSize = "left"; // "left" interferes with calculations for button placement
        mc.choiceTxt.autoSize = "true";
        mc.indexNum = i;
        mc._y = yPosition;
        yPosition += mc._height + this.choiceBuffer;
   }

you were not declaring the yPosition variable, probably why it traces NotANumber…
and in the end, i changed the yPosition to set his value with the hole mc, not just the text.