A Flash Developer Resource Site

Results 1 to 6 of 6

Thread: Display Score Issue

  1. #1
    Senior Member
    Join Date
    Jan 2008
    Posts
    107

    Display Score Issue

    Hi all,

    I'm trying to create a simple script. You press a button and you have 50% chances of doubling your current score. If you lose, you lose 100 gold/points. To achieve this I used the Math.random() function to give me two options: 1 (win) and 2 (lose).

    I have a dynamic text called "total" where it displays your current score.

    The problem is, I don't know why the score doesn't update when you press the button. Here's the code:

    PHP Code:
    import flash.events.MouseEvent;
    import flash.events.Event;

    stop();


    var 
    coins:int 100000000;
    var 
    coinsString:String String(coins);
        
        
    bet100.addEventListener(MouseEvent.CLICKgamble);

    function 
    gamble(event:MouseEvent):void
    {
        var 
    lucky:Number Math.floor(Math.random()*2)+1;
        
        if(
    lucky == 1)
        {
            
    //trace("YOU WIN!");
            
    status.text String("You WON +200 GOLD!");
            
    coins += 200;
        }
        else
        {
            
    status.text String("You lost 100 gold...");
            
    coins -= 100;
        }
    }

    total.text coinsString

  2. #2
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    Actionscript Code:
    import flash.events.MouseEvent;
    import flash.events.Event;

    stop();

    var coins:int = 100000000;
    var coinsString:String;
    updateTxt();

    bet100.addEventListener(MouseEvent.CLICK, gamble);

    function gamble(event:MouseEvent):void {
        var lucky:Number = Math.floor(Math.random()*2)+1;

        if (lucky == 1) {
            //trace("YOU WIN!");
            status.text = String("You WON +200 GOLD!");
            coins += 200;
            updateTxt();
        } else {
            status.text = String("You lost 100 gold...");
            coins -= 100;
            updateTxt();
        }
       
    }

    function updateTxt() {
        coinsString = String(coins);
        total.text = coinsString;
    }



    arkitx
    Last edited by arkitx; 08-04-2011 at 07:33 PM.

  3. #3
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    Thanks for the quick reply arkitx. That worked great.

    Just one more question. If I'm interested in adding additional buttons (Bet 500 gold, Bet 1000 gold...), how can I build a single function instead of copy/pasting the gamble function several times?

  4. #4
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    Actionscript Code:
    import flash.events.MouseEvent;
    import flash.events.Event;

    stop();

    var coins:int = 100000000;
    var coinsString:String;
    updateTxt();

    bet100.addEventListener(MouseEvent.CLICK, gamble);
    bet200.addEventListener(MouseEvent.CLICK, gamble);
    bet500.addEventListener(MouseEvent.CLICK, gamble);
    bet1000.addEventListener(MouseEvent.CLICK, gamble);


    function gamble(event:MouseEvent):void {
        switch (event.target.name) {
            case "bet100" :
                bet(100);
                break;
            case "bet200" :
                bet(200);
                break;
            case "bet500" :
                bet(500);
                break;
            case "bet1000" :
                bet(1000);
                break;
        }
    }
    function bet(amount:int) {
        var lucky:Number = Math.floor(Math.random()*2)+1;
        if (lucky == 1) {
            //trace("YOU WIN!");
            status.text = String("You WON +"+amount+" GOLD!");
            coins += amount;
            updateTxt();
        } else {
            status.text = String("You lost "+amount/2+" gold...");
            coins -= amount/2;
            updateTxt();
        }
    }

    function updateTxt() {
        coinsString = String(coins);
        total.text = coinsString;
    }




    arkitx

  5. #5
    Senior Member
    Join Date
    Jan 2008
    Posts
    107
    That is much simpler than what I was doing.

    Thanks for the help.

  6. #6
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    If you are stepping to make a gambling game in Flash. Then I must suggest you to read some casino law and flaws and also the method of making games like Gambling. Take it serious, cause every knowledge must go through the right way.



    arkitx

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center