Skip to main content
Participant
April 3, 2016
Answered

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

  • April 3, 2016
  • 2 replies
  • 669 views

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

This topic has been closed for replies.
Correct answer kglad

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


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);

}

2 replies

kglad
Community Expert
Community Expert
April 3, 2016

each time one is changed call:

function addAllF():void{

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

}

robdillon
Participating Frequently
April 3, 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);

tanis95Author
Participant
April 3, 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.

kglad
Community Expert
Community Expert
April 3, 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();

}