Adding numbers

Hi
I need your help with following problems!!!
How to add numbers??
I have textfield with instance “field1” and two buttons with following script
onRelease = function(){
_root.field.text =1;
}

how do I add the sum when second button clicked and not insert value 1 again?

I tried this way but didn`t work
onRelease = function(){
_root.field.text =add.1;
}

thanks
green

http://www.n99creations.com/?pID=tutorials&col=Blue&tut=basics_of_actionscript_for_flash&p=1&l=Flash_MX_04

onRelease = function(){
_root.field.text += 1;
}

hi greggeh
it gives 11

and here is the code
c1.onMouseDown = function() {
_root.field.text += 1;
};

…maybe I didn`t specify my problem clearly…sorry for that

button1 holds a value of 1
button2 also hold the value of 1

when button1 is released…value one is inserted in textfield
now when button2 is released…the textfield should have value of 2
…adding the sum…from previous state.

best way would be like… on frame


field.text=myVar = 0;
c1.onPress = function(){
field.text=myVar+=1;
};

this is because when using the .text value(instance names), flash doesnt like adding, unless you use field.text++; but you should use += because then you can add not only one, when only adding one however, use ++ because it’s faster;)

still not working:(
field.text = myVar=0;
c1.onMouseDown = function() {
field.text = myVar += 1;
trace (myVar);
};
// output 2…how come? when myVar set to 0 and adding 1 to it would be 1

just try onPress or onRelease instead of onMouseDown…

the text field stores it as a string, not a number. convert it to a number first then add.


myVar=0;
//then set the text to myVar
field.text = myVar;
c1.onMouseDown = function() {
myVar++;
field.text = myVar;
trace (myVar);
};

You don’t need the extra variable, just cast the string as a number like mprzybylski said:

field.text = 0;
c1.onPress = function():Void  {
 field.text = Number(field.text) + 1;
};

:thumb: good point …

finally…many thanks folks!