Hi.
I’ve got a list in a flash app that when an item is clicked it should add a number to a dynamic text field, my problem is that the variable I declare to store the number in either always resets to 0/ doesn’t add the numbers or returns NaN.
The variable in question is
var totalNum:Number;
What I want it to do is add the numbers when a list item is selected, e.g. totalNum starts at 0, if item 1 is selected totalNum should now be 1, if item2 is then selected totalNum should be (1+2 = 3), instead the label just displays 1, then 2.
I’ve tried declaring the variable outside of the event, but that resulted in the label showing NaN, unfortunately (as far as my understanding goes) declaring it inside makes it a local variable which resets each time the event is called and I’m not sure how to get around this (I can’t seem to get the syntax for global variables right)
An example of my code is:
list1.addEventListener(Event.CHANGE, ItemChange);
function ItemChange(event:Event) {
var Item = event.target.selectedItem.data;
var totalNum:Number;
if (Item == "1"){
totalNum + 1;
}
else if (Item == "2"){
totalNum + 2;
}
else if (Item == "3"){
totalNum + 3;
}
else if (Item == "4"){
totalNum + 4;
TotalValuelabel.text = totalNum.toString();
}