Copy link to clipboard
Copied
Hi:
I need some community help with a small section of my actionscript code. Everything is working fine in my published file except the fact that I when I use the NumberFormatter class to add commas to my dynamic text fields no commas show up to separate the numbers. I have no syntax errors when I run Test Movie. Everything seems to be loading and running well, but when I display the numbers on stage no commas show up. Can someone help me figure this out why the actionscript is not executing?
Here is the code that I am using on two different frames
BIRTHDATE FRAME (Frame 1)
stop();
//FULL SCREEN MODE
//stage.displayState = StageDisplayState.FULL_SCREEN;
//CALENDAR DATE GLOBAL VARIABLES INPUT VARIABLES
var stringBDM: String; //birth month text
var stringBDD: String; //birthday text
var stringBDY: String; //birth year
var BDMNum: Number; //birth month number
var BDDNum: Number; //birthday number
var BDYNum: Number; //birth year number
var BYS: Number; //birthday years number of years lived
var BDS: Number; //birthdays number of days lived
var CBY: Number; //corrected birthday years converted to number variable
var CBD: Number; //corrected birthdays converted to number variable
var stringTDM: String; //current month (today) text
var stringTDD: String; //current day (today) text
var stringTDY: String; //current year (today) text
var TDMNum: Number; //current month (today) number
var TDDNum: Number; //current day (today) number
var TDYNum: Number; //current year (today) number
//LIFETIME AGE GLOBAL VARIABLES
var LDSNum: Number = 0; //lifetime days
var LHSNum: Number = 0; //lifetime hours
var LMSNum: Number = 0; //lifetime minutes
var LSSNum: Number = 0; //lifetime seconds
//var LMSS: Number = 0; //lifetime milliseconds
var stringLDS: String; //lifetime days text string for output display
var stringLHS: String; //lifetime hours text string for output display
var stringLMS: String; //lifetime minutes text string for output display
var stringLSS: String; //lifetime seconds text string for output display
var alphaADJ: Number = 0; //alpha adjustment
var omegaADJ: Number = 0; //omega adjustment
var alphaMonth: Number; //alpha calendar month
var omegaMonth: Number; //omega calendar month
var TLY: Number = 0; //total leap years
var TLYABS: Number = 0; //total leap years absolute value
var numForm: NumberFormatter = new NumberFormatter(LocaleID.DEFAULT);
//COSMIC AGE GLOBAL VARIABLES
var CDSNum: Number = 0; //cosmic days
var CHSNum: Number = 0; //cosmic hours
var CMSNum: Number = 0; //cosmic minutes
var CSSNum: Number = 0; //cosmic seconds
//var CMSS: Number = 0; //cosmic milliseconds
var stringCDS: String; //cosmic days text string for output display
var stringCHS: String; //cosmic hours text string for output display
var stringCMS: String; //cosmic minutes text string for output display
var stringCSS: String; //cosmic seconds text string for output display
//STANDARD TIME CONSTANTS GLOBAL VARIABLES
var calYear: Number = 365; //days in a year or total calendar days (TCD)
var calMonth: Number = 12; //months in a year
var calWeek: Number = 7; //days in a week
var calDay: Number = 24; //hours in a day
var numHr: Number = 60; //minutes in an hour
//trace(numHr);
var numMin: Number = 60; //seconds in a minute
//trace(numMin);
var numSec: Number = 1000; //milliseconds in a second
//trace(numSec);
var calLeapYear: Number = 1; //days in a leap year, which occurs every 4 years
//COSMIC TIME CONSTANTS GLOBAL VARIABLES
var gConstant: int = 1000; //number of God years in a day
var R1: Number = 1 / gConstant; //ratio of earth days to God years
//trace(R1);
var R2: Number = calYear / gConstant; //ratio of number of days in a year to God years
//trace(R2);
var eFactor: Number = R1 / R2; //ether factor (lifeforce)
eFactor = Math.floor(eFactor * 1000000) / 1000000 //round EF to five decimal points
//trace(eFactor);
var cosmicDayYear: Number = calYear * eFactor; //cosmic day per year or (cosmic day year)
//trace(cosmicDayYear);
//var CY:int = 0; //cosmic year(s)
var cosmicWeek: Number = calWeek * eFactor; //cosmic week
cosmicWeek = Math.floor(cosmicWeek * 1000000) / 1000000 //round cosmicWeek to five decimal points
//trace(cosmicWeek);
var cosmicDay: Number = eFactor; //cosmic days
//trace(cosmicDay);
var cosmicHr: Number = numHr * eFactor; //cosmic hours
//trace(cosmicHr);
var cosmicMin: Number = numMin * eFactor; //cosmic minutes
//trace(cosmicMin);
var cosmicSec: Number = numSec * eFactor; //cosmic seconds
cosmicSec = Math.floor(cosmicSec * 1000000) / 1000000 //round cosmicMillSec to five decimal points
//trace(cosmicSec);
//CALENDAR MONTH ADJUSMENT FACTORS GLOBAL VARIABLES
var alphaCMAF: int; //alpha calendar month adjustment factor
var omegaCMAF: int; //alpha calendar month adjustment factor
var CMAF1: int = 31; //number of days available for accural in January
var CMAF2: int = 59; //number of days available for accural in February
var CMAF3: int = 90; //number of days available for accural in March
var CMAF4: int = 120; //number of days available for accural in April
var CMAF5: int = 151; //number of days available for accural in May
var CMAF6: int = 181; //number of days available for accural in June
var CMAF7: int = 212; //number of days available for accural in July
var CMAF8: int = 243; //number of days available for accural in August
var CMAF9: int = 273; //number of days available for accural in September
var CMAF10: int = 304; //number of days available for accural in October
var CMAF11: int = 334; //number of days available for accural in November
var CMAF12: int = 365; //number of days available for accural in December
//SELECT ALPHA ADJUSTMENT CALENDAR MONTH FACTOR
function alphaSwitch(): void {
switch (alphaMonth) {
case 1:
alphaCMAF = CMAF1;
trace(alphaCMAF);
break;
case 2:
alphaCMAF = CMAF2;
trace(alphaCMAF);
break;
case 3:
alphaCMAF = CMAF3;
trace(alphaCMAF);
break;
case 4:
alphaCMAF = CMAF4;
trace(alphaCMAF);
break;
case 5:
alphaCMAF = CMAF5;
trace(alphaCMAF);
break;
case 6:
alphaCMAF = CMAF6;
trace(alphaCMAF);
break;
case 7:
alphaCMAF = CMAF7;
trace(alphaCMAF);
break;
case 8:
alphaCMAF = CMAF8;
trace(alphaCMAF);
break;
case 9:
alphaCMAF = CMAF9;
trace(alphaCMAF);
break;
case 10:
alphaCMAF = CMAF10;
trace(alphaCMAF);
break;
case 11:
alphaCMAF = CMAF11;
trace(alphaCMAF);
break;
case 12:
alphaCMAF = CMAF12;
trace(alphaCMAF);
break;
default:
trace("Invalid data");
break;
}
}
//SELECT OMEGA ADJUSTMENT CALENDAR MONTH FACTOR
function omegaSwitch(): void {
switch (omegaMonth) {
case 1:
omegaCMAF = CMAF1;
trace(omegaCMAF);
break;
case 2:
omegaCMAF = CMAF2;
trace(omegaCMAF);
break;
case 3:
omegaCMAF = CMAF3;
trace(omegaCMAF);
break;
case 4:
omegaCMAF = CMAF4;
trace(omegaCMAF);
break;
case 5:
omegaCMAF = CMAF5;
trace(omegaCMAF);
break;
case 6:
omegaCMAF = CMAF6;
trace(omegaCMAF);
break;
case 7:
omegaCMAF = CMAF7;
trace(omegaCMAF);
break;
case 8:
omegaCMAF = CMAF8;
trace(omegaCMAF);
break;
case 9:
omegaCMAF = CMAF9;
trace(omegaCMAF);
break;
case 10:
omegaCMAF = CMAF10;
trace(omegaCMAF);
break;
case 11:
omegaCMAF = CMAF11;
trace(omegaCMAF);
break;
case 12:
omegaCMAF = CMAF12;
trace(omegaCMAF);
break;
default:
trace("Invalid data");
break;
}
}
//CHECK MOUSE CLICK
import flash.events.MouseEvent;
function goClick(event: MouseEvent): void {
trace("Birthdate");
//INPUT BIRTH DATE AND TODAY'S DATE
stringBDM = BDM.text;
stringBDD = BDD.text;
stringBDY = BDY.text;
//trace(stringBDM);
//trace(stringBDD);
//trace(stringBDY);
stringTDM = TDM.text;
stringTDD = TDD.text;
stringTDY = TDY.text;
//trace(stringTDM);
//trace(stringTDD);
//trace(stringTDY);
//PROCESS BIRTH DATE AND TODAY'S DATE
BDMNum = numForm.parseNumber(stringBDM); //convert, parse, and assign birth month to this variable
BDDNum = numForm.parseNumber(stringBDD); //convert, parse, and assign birthday number variable
BDYNum = numForm.parseNumber(stringBDY); //convert, [arse, and assign birth year converted to number
trace(BDMNum);
trace(BDDNum);
trace(BDYNum);
TDMNum = numForm.parseNumber(stringTDM); //current month (today) number variable
TDDNum = numForm.parseNumber(stringTDD); //current day (today) numbe variable
TDYNum = numForm.parseNumber(stringTDY); //current year (today) number variable
trace(TDMNum);
trace(TDDNum);
trace(TDYNum);
//CALCULATE INFINITY VARIABLES
alphaMonth = BDMNum;
omegaMonth = TDMNum;
alphaSwitch();
omegaSwitch();
BYS = TDYNum - BDYNum;
BDS = BYS * calYear;
CBY = BYS - 1;
CBD = CBY * calYear;
alphaADJ = calYear - alphaCMAF - BDDNum;
omegaADJ = omegaCMAF + TDDNum;
TLY = (TDYNum - BDYNum) / 4;
TLYABS = (TLY);
trace(BYS);
trace(BDS);
trace(CBY);
trace(CBD);
trace(alphaADJ);
trace(omegaADJ);
trace(TLY);
trace(TLYABS);
gotoAndStop("Lifetime Age");
}
goBtn.addEventListener(MouseEvent.CLICK, goClick);
LIFETIME AGE (FRAME 30)
//CALCULATE LIFETIME AGE
LDSNum = Math.floor(CBD + alphaADJ + omegaADJ + TLY);
LHSNum = LDSNum * calDay;
LMSNum = LHSNum * numHr;
LSSNum = LMSNum * numMin;
//DISPLAY LIFETIME AGE VALUES
stringLDS = String(LDSNum);
stringLHS = String(LHSNum);
stringLMS = String(LMSNum);
stringLSS = String(LSSNum);
LDS.text = stringLDS;
LHS.text = stringLHS;
LMS.text = stringLMS;
LSS.text = stringLSS;
trace("Lifetime Age");
trace(LDSNum);
trace(LHSNum);
trace(LMSNum);
trace(LSSNum);
/*
trace(stringLDS);
trace(stringLHS);
trace(stringLMS);
trace(stringLSS);
*/
//CHECK MOUSE CLICK
function nextClick(event:MouseEvent):void
{
gotoAndStop("Cosmic Age");
}
nextBtn.addEventListener(MouseEvent.CLICK, nextClick);
The code works perfectly, but when I click the goBtn to go to the LIFETIME AGE frame and display the numbers. The numbers show up, but the commas don't. Any help will be greatly appreciated.
Thanks,
Shinshuri
i don't know what lines of code you think will add commas but parseNumber() does not. if you're trying to convert a number to a string (with commas), use formatNumber()
if you're trying to convert a string (eg, stringBDM - that's not easily converted to a number by casting it) to some other string, what does stringBDM look like and in what way do you want to convert it?
Copy link to clipboard
Copied
i don't know what lines of code you think will add commas but parseNumber() does not. if you're trying to convert a number to a string (with commas), use formatNumber()
if you're trying to convert a string (eg, stringBDM - that's not easily converted to a number by casting it) to some other string, what does stringBDM look like and in what way do you want to convert it?
Copy link to clipboard
Copied
@kglad Thank you for your response. I added the following code to the LIFETIME AGE frame and it worked perfectly. Thank you! Thank you! Thank you!
NEW CODE ADDED
//PARSE LIFETIME AGE NUMBERS FROM TEXT STRINGS
var parsedLDS:Number = numForm.parseNumber(stringLDS);
var parsedLHS:Number = numForm.parseNumber(stringLHS);
var parsedLMS:Number = numForm.parseNumber(stringLMS);
var parsedLSS:Number = numForm.parseNumber(stringLSS);
//FORMAT LIFETIME AGE NUMBERS WITH COMMAS
var LDSNumCommas:String = numForm.formatNumber(parsedLDS);
var LHSNumCommas:String = numForm.formatNumber(parsedLHS);
var LMSNumCommas:String = numForm.formatNumber(parsedLMS);
var LSSNumCommas:String = numForm.formatNumber(parsedLSS);
trace(parsedLDS);
trace(LDSNumCommas);
//DISPLAY LIFETIME AGE NUMBERS AS TEXT STRINGS WITH COMMAS
LDS.text = LDSNumCommas;
LHS.text = LHSNumCommas;
LMS.text = LMSNumCommas;
LSS.text = LSSNumCommas;
Thanks for your help. Outstanding!
Shinshuri
Copy link to clipboard
Copied
you're welcome.
p.s. by accural do you mean accrual?
p.p.s. read the previous message for help improving your code.
Copy link to clipboard
Copied
@ClayUUID. Yes. The information you shared is a more efficient way to code. This project is in pilot mode right now. I am planning out the details for a much larger mobile app to support new product concepts. But, I wanted to test out the concept first. So I was not focused on code efficiency just yet. Also, thanks for the Leap Year tip. I will have to look over the AS3 API Reference to see how it may align with the mathematical concepts we are attempting to build to see if it will work for the calculation of Cosmic Age.
Thank you for the insight on how to better structure and simplify the code.
Shinshuri
Copy link to clipboard
Copied
kglad​ not sure why you are asking about accrual. ClayUUID's tips are fantastic. I am grateful for the additional insight. Thanks to both of you.
Shinshuri
Copy link to clipboard
Copied
you have it in your comments.
Copy link to clipboard
Copied
Your immediate problem aside, you really need to learn how to use arrays (in this case vectors would be even better, but they're somewhat more complex to instantiate). This entire chunk of code:
var CMAF1: int = 31; //number of days available for accural in January
var CMAF2: int = 59; //number of days available for accural in February
var CMAF3: int = 90; //number of days available for accural in March
var CMAF4: int = 120; //number of days available for accural in April
var CMAF5: int = 151; //number of days available for accural in May
var CMAF6: int = 181; //number of days available for accural in June
var CMAF7: int = 212; //number of days available for accural in July
var CMAF8: int = 243; //number of days available for accural in August
var CMAF9: int = 273; //number of days available for accural in September
var CMAF10: int = 304; //number of days available for accural in October
var CMAF11: int = 334; //number of days available for accural in November
var CMAF12: int = 365; //number of days available for accural in December
...could be replaced with this:
// number of days available for accrual in each month
var CMAF:Array = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365];
Which would allow this entire function to be eliminated:
//SELECT ALPHA ADJUSTMENT CALENDAR MONTH FACTOR
function alphaSwitch(): void {
switch (alphaMonth) {
case 1:
alphaCMAF = CMAF1;
trace(alphaCMAF);
break;
case 2:
alphaCMAF = CMAF2;
trace(alphaCMAF);
break;
case 3:
alphaCMAF = CMAF3;
trace(alphaCMAF);
break;
etc...
...and replaced with just this:
alphaCMAF = CMAF[alphaMonth];
And likewise, this entire function:
//SELECT OMEGA ADJUSTMENT CALENDAR MONTH FACTOR
function omegaSwitch(): void {
switch (omegaMonth) {
case 1:
omegaCMAF = CMAF1;
trace(omegaCMAF);
break;
case 2:
omegaCMAF = CMAF2;
trace(omegaCMAF);
break;
case 3:
omegaCMAF = CMAF3;
trace(omegaCMAF);
break;
etc...
...could be replaced with:
omegaCMAF = CMAF[omegaMonth];
On another subject, you should really be using the AS3 Date library instead of trying to calculate leap years and all that yourself. Whatever you've come up with is almost certainly going to return wrong results in some circumstances. Dates are hard. Leave them to the experts.
Date - Adobe ActionScript® 3 (AS3 ) API Reference
On the other hand, maybe I shouldn't be looking too closely at code that includes lines like "CALCULATE INFINITY VARIABLES" and "God years in a day".
Find more inspiration, events, and resources on the new Adobe Community
Explore Now