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

Commas for numbers in script.

Explorer ,
May 26, 2015 May 26, 2015

Hello!

So I am building a program in flash that will run a jeopardy scoreboard just like the C# file at this website (Jeopardy! | Coding4Fun Articles | Channel 9) as it is cheaper for me to build it in flash than to pay $80+ for a Phidget. Everything has been going fine. However, I can't get the string to add commas for the life of me! I've tried the NumberFormatter class, but nothing happens. I tried to import it but it doesn't work nor do I get code building hints. I would say my knowledge of AS3 is ahead of a N00B, but not yet intermediate. I'm doing this purely for a hobby.

This code is below. I am working on the first player and the $200:button, so everything here is in relation to the first player's score and the +/- $200 functions:

var firstPlayerScore:int;

firstPlayerScore = 0

var questionValue:Number = 0;

p1Score.text = "$"+String(firstPlayerScore);

p1200btn.addEventListener(MouseEvent.CLICK, firstNumber);

p1addbtn.addEventListener(MouseEvent.CLICK, addP1);

p1minusbtn.addEventListener(MouseEvent.CLICK, dropP1);

function firstNumber(Event:MouseEvent):void {

    questionValue = 200

}

function addP1(Event:MouseEvent):void {

    firstPlayerScore = firstPlayerScore + questionValue;

        p1Score.text = "$"+String(firstPlayerScore);

    questionValue = 0

}

function dropP1(Event:MouseEvent):void {

    firstPlayerScore = firstPlayerScore - questionValue;

        p1Score.text = "$"+String(firstPlayerScore);

    questionValue = 0

}

//End code

Any help would be greatly appreciated. Thanks!

TOPICS
ActionScript
349
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

LEGEND , May 26, 2015 May 26, 2015

Since both your functions repeat the same code you can probably create another function for them both to share, and extending this further, by passing the textfield andf score as arguments you could share it for all players...

function addP1(Event:MouseEvent):void {

    firstPlayerScore = firstPlayerScore + questionValue;

    writeScore(p1Score, firstPlayerScore);

}

function dropP1(Event:MouseEvent):void {

    firstPlayerScore = firstPlayerScore - questionValue;

    writeScore(p1Score, firstPlayerScore

...
Translate
LEGEND ,
May 26, 2015 May 26, 2015

Since both your functions repeat the same code you can probably create another function for them both to share, and extending this further, by passing the textfield andf score as arguments you could share it for all players...

function addP1(Event:MouseEvent):void {

    firstPlayerScore = firstPlayerScore + questionValue;

    writeScore(p1Score, firstPlayerScore);

}

function dropP1(Event:MouseEvent):void {

    firstPlayerScore = firstPlayerScore - questionValue;

    writeScore(p1Score, firstPlayerScore);

}

function writeScore(tField:TextField, score:int):void {

    var commaScore:String = .... add the commas here however you intend to do it

    tField.text = "$"+ commaScore;

    questionValue = 0

}

If you want help with the NumberFormatter you will need to provide some info about it.

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
Explorer ,
May 26, 2015 May 26, 2015

Thanks for your reply!

function writeScore(tField:TextField, score:int):void {

    var commaScore:String = .... add the commas here however you intend to do it

    tField.text = "$"+ commaScore;

    questionValue = 0

}

I think my noob-ness is a factor here: I think I would put in the code like I have below, but I haven't found a clear description of how to actually deal with commas. Everything I found is usually for folks with more background than me and they expect that full understanding previously. I saw a few if statements that created a bunch of extra variables, but I can honestly say that I'm confused for the first time. I'm sad that it is something so simple. Is there a good place to read and/or the logic behind the code for commas?

function writeScore(tField TextField, p1Score:int):void {

    var commaScore:String = ????

    p1Score.text = "$"+ commaScore;

    questionValue = 0

}

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
LEGEND ,
May 26, 2015 May 26, 2015

You should learn how to use Google to search for code help.  The search terms "AS3 number comma" led to a posting that offers this...

function addComma(num:uint):String {

    var str:String="";

    while(num>0){

        var tmp:uint=num%1000;

        str=(num>999?","+(tmp<100?(tmp<10?"00":"0"):""):"")+tmp+str;

        num=num/1000;

    }

    return str;

}

so you can change the writeScore function to be.... (don't change it like you did, make it so that all players can share it)

function writeScore(tField:TextField, score:int):void {

    tField.text = "$"+ addComma(score);

    questionValue = 0

}

I am only assuming the function adds commas as hoped.  If not, try the search yourself as there are other results you can try.

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
Explorer ,
May 26, 2015 May 26, 2015
LATEST

That was one of the first functions I used and had issues with it.

After I posted my earlier reply I played around with my code and I figured out that my problem was that I had it as its own separate function and not a part of the writeScore function.

Thanks for you help.

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