Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How would I add together number values inside dynamic text boxes?

New Here ,
Apr 03, 2016 Apr 03, 2016

I'm making an app and I currently have sections such as Travel, rent etc where you can press one of two buttons to increase or decrease the total number (expense) for that section. All of the totals are displayed in dynamic text boxes - here is an example of the code for one section -

var FoodAndDrink:Number=0;

button1_btn.addEventListener(MouseEvent.MOUSE_DOWN, Add);

function Add(e:MouseEvent):void {

  FoodAndDrink+=1;

  trace(FoodAndDrink);

  FoodAndDrink_txt.text="£"+FoodAndDrink;

}

button2_btn.addEventListener(MouseEvent.MOUSE_DOWN, Subtract);

function Subtract(e:MouseEvent):void {

  FoodAndDrink-=1;

  if (FoodAndDrink < 0) {

  FoodAndDrink = 0;

  }

  trace(FoodAndDrink);

  FoodAndDrink_txt.text="£"+FoodAndDrink;

Does anybody know how I would go about adding together all of the sections so that the total is in one text box and changes based on the inputted total in each section?

Thanks

TOPICS
ActionScript
604
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Apr 03, 2016 Apr 03, 2016

then use:

import flash.events.MouseEvent;

var FoodAndDrink:Number=0;

button1_btn.addEventListener(MouseEvent.MOUSE_DOWN, Add);

function Add(e:MouseEvent):void {

  FoodAndDrink+=1;

  trace(FoodAndDrink);

  FoodAndDrink_txt.text="£"+FoodAndDrink;

addAllF();

}

button2_btn.addEventListener(MouseEvent.MOUSE_DOWN, Subtract);

function Subtract(e:MouseEvent):void {

  FoodAndDrink-=1;

  if (FoodAndDrink < 0) {

  FoodAndDrink = 0;

  }

  trace(FoodAndDrink);

  FoodAndDrink_txt.text="£"+FoodAndDrink;

...
Translate
LEGEND ,
Apr 03, 2016 Apr 03, 2016

If you are keeping all of the various values in variables, just add up the contents of the variables and then display that result in a textfield as you do the other values.

var var1:Number = 1;

var var2:Number = 2;

var var3:Number = (var1 + var2);

trace(var3);

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 03, 2016 Apr 03, 2016

I'm new to coding to I don't understand how I would do this if the numbers within each section aren't predefined? They go up and down depending on what the user would input.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2016 Apr 03, 2016

again,

each time one is changed call:

function addAllF():void{

total_txt.text='pound sign'+(FoodAndDrink+WhateverElse+OtherCategories);

}

eg,

function Add(e:MouseEvent):void {

  FoodAndDrink+=1;

  trace(FoodAndDrink);

  FoodAndDrink_txt.text="£"+FoodAndDrink;

addAllF();

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 03, 2016 Apr 03, 2016

Apologies for being dumb lmao, I just really don't understand this  

This is the full set of code -

import flash.events.MouseEvent;

var FoodAndDrink:Number=0;

button1_btn.addEventListener(MouseEvent.MOUSE_DOWN, Add);

function Add(e:MouseEvent):void {

  FoodAndDrink+=1;

  trace(FoodAndDrink);

  FoodAndDrink_txt.text="£"+FoodAndDrink;

}

button2_btn.addEventListener(MouseEvent.MOUSE_DOWN, Subtract);

function Subtract(e:MouseEvent):void {

  FoodAndDrink-=1;

  if (FoodAndDrink < 0) {

  FoodAndDrink = 0;

  }

  trace(FoodAndDrink);

  FoodAndDrink_txt.text="£"+FoodAndDrink;

}

var Entertainment:Number=0;

button3_btn.addEventListener(MouseEvent.MOUSE_DOWN, Add1);

function Add1(e:MouseEvent):void {

  Entertainment+=1;

  trace(Entertainment);

  Entertainment_txt.text="£"+Entertainment;

}

button4_btn.addEventListener(MouseEvent.MOUSE_DOWN, Subtract1);

function Subtract1(e:MouseEvent):void {

  Entertainment-=1;

  if (Entertainment < 0) {

  Entertainment = 0;

  }

  trace(Entertainment);

  Entertainment_txt.text="£"+Entertainment;

}

var Accommodation:Number=0;

button5_btn.addEventListener(MouseEvent.MOUSE_DOWN, Add2);

function Add2(e:MouseEvent):void {

  Accommodation+=1;

  trace(Accommodation);

  Accommodation_txt.text="£"+Accommodation;

}

button6_btn.addEventListener(MouseEvent.MOUSE_DOWN, Subtract2);

function Subtract2(e:MouseEvent):void {

  Accommodation-=1;

  if (Accommodation < 0) {

  Accommodation = 0;

  }

  trace(Accommodation);

  Accommodation_txt.text="£"+Accommodation;

}

var Travel:Number=0;

button7_btn.addEventListener(MouseEvent.MOUSE_DOWN, Add3);

function Add3(e:MouseEvent):void {

  Travel+=1;

  trace(Travel);

  Travel_txt.text="£"+Travel;

}

button8_btn.addEventListener(MouseEvent.MOUSE_DOWN, Subtract3);

function Subtract3(e:MouseEvent):void {

  Travel-=1;

  if (Travel < 0) {

  Travel = 0;

  }

  trace(Travel);

  Travel_txt.text="£"+Travel;

}

var Bills:Number=0;

button9_btn.addEventListener(MouseEvent.MOUSE_DOWN, Add4);

function Add4(e:MouseEvent):void {

  Bills+=1;

  trace(Bills);

  Bills_txt.text="£"+Bills;

}

button10_btn.addEventListener(MouseEvent.MOUSE_DOWN, Subtract4);

function Subtract4(e:MouseEvent):void {

  Bills-=1;

  if (Bills < 0) {

  Bills = 0;

  }

  trace(Bills);

  Bills_txt.text="£"+Bills;

}

Is your reply an addition to the other user, or? Ah, I hate feeling so stupid with this

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2016 Apr 03, 2016

is that correct? everything costs 1 pound?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 03, 2016 Apr 03, 2016

There are buttons on the stage that allow the user to add/subtract as much as they like to the total for each section, and it currently goes up in 1's and cannot go below 0. Everything works okay and I receive no errors. The total for each category isn't defined

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2016 Apr 03, 2016

then use:

import flash.events.MouseEvent;

var FoodAndDrink:Number=0;

button1_btn.addEventListener(MouseEvent.MOUSE_DOWN, Add);

function Add(e:MouseEvent):void {

  FoodAndDrink+=1;

  trace(FoodAndDrink);

  FoodAndDrink_txt.text="£"+FoodAndDrink;

addAllF();

}

button2_btn.addEventListener(MouseEvent.MOUSE_DOWN, Subtract);

function Subtract(e:MouseEvent):void {

  FoodAndDrink-=1;

  if (FoodAndDrink < 0) {

  FoodAndDrink = 0;

  }

  trace(FoodAndDrink);

  FoodAndDrink_txt.text="£"+FoodAndDrink;

addAllF();

}

var Entertainment:Number=0;

button3_btn.addEventListener(MouseEvent.MOUSE_DOWN, Add1);

function Add1(e:MouseEvent):void {

  Entertainment+=1;

  trace(Entertainment);

  Entertainment_txt.text="£"+Entertainment;

addAllF();

}

button4_btn.addEventListener(MouseEvent.MOUSE_DOWN, Subtract1);

function Subtract1(e:MouseEvent):void {

  Entertainment-=1;

  if (Entertainment < 0) {

  Entertainment = 0;

  }

  trace(Entertainment);

  Entertainment_txt.text="£"+Entertainment;

addAllF();

}

var Accommodation:Number=0;

button5_btn.addEventListener(MouseEvent.MOUSE_DOWN, Add2);

function Add2(e:MouseEvent):void {

  Accommodation+=1;

  trace(Accommodation);

  Accommodation_txt.text="£"+Accommodation;

addAllF();

}

button6_btn.addEventListener(MouseEvent.MOUSE_DOWN, Subtract2);

function Subtract2(e:MouseEvent):void {

  Accommodation-=1;

  if (Accommodation < 0) {

  Accommodation = 0;

  }

  trace(Accommodation);

  Accommodation_txt.text="£"+Accommodation;

addAllF();

}

var Travel:Number=0;

button7_btn.addEventListener(MouseEvent.MOUSE_DOWN, Add3);

function Add3(e:MouseEvent):void {

  Travel+=1;

  trace(Travel);

  Travel_txt.text="£"+Travel;

addAllF();

}

button8_btn.addEventListener(MouseEvent.MOUSE_DOWN, Subtract3);

function Subtract3(e:MouseEvent):void {

  Travel-=1;

  if (Travel < 0) {

  Travel = 0;

  }

  trace(Travel);

  Travel_txt.text="£"+Travel;

addAllF();

}

var Bills:Number=0;

button9_btn.addEventListener(MouseEvent.MOUSE_DOWN, Add4);

function Add4(e:MouseEvent):void {

  Bills+=1;

  trace(Bills);

  Bills_txt.text="£"+Bills;

addAllF();

}

button10_btn.addEventListener(MouseEvent.MOUSE_DOWN, Subtract4);

function Subtract4(e:MouseEvent):void {

  Bills-=1;

  if (Bills < 0) {

  Bills = 0;

  }

  trace(Bills);

  Bills_txt.text="£"+Bills;

addAllF();

}

function addAllF():void{

total_txt.text='pound sign'+(FoodAndDrink+Bills+Travel+Accommodation+Entertainment);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Apr 03, 2016 Apr 03, 2016

This worked perfectly! Thanks a lot

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2016 Apr 03, 2016
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Apr 03, 2016 Apr 03, 2016

each time one is changed call:

function addAllF():void{

total_txt.text='pound sign'+(FoodAndDrink+WhateverElse+OtherCategories);

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines