Copy link to clipboard
Copied
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
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;
Copy link to clipboard
Copied
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);
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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();
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
is that correct? everything costs 1 pound?
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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);
}
Copy link to clipboard
Copied
This worked perfectly! Thanks a lot
Copy link to clipboard
Copied
you're welcome.
Copy link to clipboard
Copied
each time one is changed call:
function addAllF():void{
total_txt.text='pound sign'+(FoodAndDrink+WhateverElse+OtherCategories);
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now