-
Animal Flasher
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
-
newb of many sorts
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.
-
Animal Flasher
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
-
newb of many sorts
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.
-
Animal Flasher
Brilliant! thanks mate
-
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
-
Originally Posted by shiz23
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|