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

Actionscript 3 Button that counts clicks

Community Beginner ,
Apr 29, 2014 Apr 29, 2014

I know I am a total noob to flash, but is there a way to make a button that has a number that goes up each time you click it in actionscript 3? If so please HELP ME!!!

TOPICS
ActionScript
4.5K
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 , Apr 29, 2014 Apr 29, 2014

If you created a button and assigned it an instance name "btn", the following code will do what you ask...

var clickCount:int = 0;

btn.addEventListener(MouseEvent.CLICK, addClick);

function addClick(evt:MouseEvent):void {

      clickCount++;

}

Translate
LEGEND ,
Apr 29, 2014 Apr 29, 2014

If you created a button and assigned it an instance name "btn", the following code will do what you ask...

var clickCount:int = 0;

btn.addEventListener(MouseEvent.CLICK, addClick);

function addClick(evt:MouseEvent):void {

      clickCount++;

}

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
Community Beginner ,
Apr 29, 2014 Apr 29, 2014

Well, where is the number of clicks supposed to show up?

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
Community Beginner ,
Apr 29, 2014 Apr 29, 2014

if you have a dynamic text, give it the instance name _clickTxt

covert clickCount in to a string

_clickTxt.text = clickCountString;

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
Community Beginner ,
Apr 29, 2014 Apr 29, 2014

Ok, so here is the code I have:

  1. stop();
  2. var clickCount:int = 0;
  3. btn.addEventListener(MouseEvent.CLICK, addClick);
  4. function addClick(event:MouseEvent):void {
  5.       clickCount++;
  6. }
  7. _clickTxt.text = " SCORE:"+clickCount.toString();

When i run the program, I see the text box says "SCORE: 0" and when I click the button nothing happens. Is there some sort of way I need to update the string after every click?

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 ,
Apr 29, 2014 Apr 29, 2014
LATEST

Yes, each time you increment the value you update the textfield too...

  • function addClick(event:MouseEvent):void {
  •       clickCount++;
  •       _clickTxt.text = " SCORE:"+clickCount.toString();
  • }
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