A Flash Developer Resource Site

Results 1 to 7 of 7

Thread: dynamic text counter on click

  1. #1
    Animal Flasher RobbieC's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    242

    dynamic text counter on click

    hi there, I've got a line of dynamic text which I want to use as a counter....

    Very simply, I have a button, and all I want is to use the dynamic text, which is initially set to '0', to count each time the button is clicked....

    I can do this in AS2 which was something like:
    Code:
    on(release){
    counter.text = counter+1;
    }
    but I'm still trying to get my head around the math side of as3

    Thanks in advance for any help

    Rob

  2. #2
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    Math works the same way in AS3 as it does in AS2. Though, I would use a separate variable to do the counting, and let counter.text reflect it. on(release) however, works differently...

    Code:
    var count:uint = 0;
    
    myBtn.addEventListener(MouseEvent.CLICK, addCount);
    counter.text = '0';
    
    function addCount(event:Event):void
    {
    	count++;
    	counter.text = count.toString();
    }
    this assumes that you have a textfield name "counter" and a button named "myBtn" on the stage.

    --- EDIT ---

    just to clarify...

    count++;

    is the same as

    count += 1;

    which is the same as

    count = count+1;
    Last edited by Ralgoth; 01-05-2010 at 09:18 AM.
    Search first, asked questions later.

  3. #3
    Animal Flasher RobbieC's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    242
    That's terrific mate, thanks!

    So, if I had 5 buttons on the stage and I wanted to use the same dynamic text to record the number of overall hits from all buttons, I can use a similar code for each button?

    I tried this, but it just shows up the individual counts for each button....

    Any ideas? Cheers

  4. #4
    newb of many sorts Ralgoth's Avatar
    Join Date
    Apr 2002
    Posts
    466
    there shouldn't be a problem with that...

    Code:
    var count:uint = 0;
    counter.text = '0';
    
    myBtn1.addEventListener(MouseEvent.CLICK, btnClick1);
    myBtn2.addEventListener(MouseEvent.CLICK, btnClick2);
    
    function btnClick1(event:MouseEvent):void
    {
    	// add code for your button here
    
    	addCount();
    }
    
    function btnClick2(event:MouseEvent):void
    {
    	// add code for your other button here
    
    	addCount();
    }
    
    function addCount():void
    {
    	count++;
    	counter.text = count.toString();
    }
    Search first, asked questions later.

  5. #5
    Animal Flasher RobbieC's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    242
    Brilliant! thanks mate

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    1
    Hi, i have tried this code with a few buttons with different input text boxes, but when i click on different buttons, it keeps adding or subtracting the amount of clicks in the other text boxes? Any help would be grateful. Cheers

  7. #7
    Member
    Join Date
    Mar 2011
    Posts
    68
    Quote Originally Posted by shiz23 View Post
    Hi, i have tried this code with a few buttons with different input text boxes, but when i click on different buttons, it keeps adding or subtracting the amount of clicks in the other text boxes? Any help would be grateful. Cheers
    Hi, im not sure about this cause i came also to check if there was any code to do such counting ....anyway...did you tried to change the instance name of each text box into a diferent name? ... im also further to test this code in a project...

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