PDA

View Full Version : why won't the variables increase in a function?


brendanb
03-18-2005, 02:00 PM
Here is my code:


_global.main1=0;
_global.main2=0;


function increaseVar(addToThis){
addToThis+=1;
}

btn1.onRelease=function(){
increaseVar(_global.main1);
}

btn2.onRelease=function(){
increaseVar(_global.main2);
}


I want different variables to increase based on pressing different buttons. I am very confused as to why this won't work. Any help would be appreciated!

Thanks,
Brendan

tg
03-18-2005, 03:27 PM
why not just:

btn1.onRelease=function(){
_global.main1++;
}

btn2.onRelease=function(){
_global.main2++;
}


you don't really need to function to do that.

brendanb
03-18-2005, 04:02 PM
But is there a way to do it with a function in case you have like 80 different buttons and you want to write a function that encompasses all of the events that happen after you press one of the buttons, including incrementing the variable?

tg
03-18-2005, 04:50 PM
_global.main1=0;
_global.main2=0;


function increaseVar(n){
//addToThis+=1;
_global["main"+n]++;
}

btn1.onRelease=function(){
increaseVar(1);
trace(_global.main1);
}

btn2.onRelease=function(){
increaseVar(2);
trace(_global.main2);
}