Actionscript 3 Second Counter (in Output)

[COLOR=red]MORE HELP NEEDED DOWN BELOW! <3[/COLOR]

Hello everyone, this is my first post on the Kirupa Forum, but won’t by my last. I’m new to scripting languages but as an aspiring graphic designer I am trying to learn flash. I’m sure this will all be very easy for most of you to fix.

Anyway, I’m trying to make a second counter that updates every second in the output window and I am pretty ■■■■ close but I can’t seem to get this to work! I need it to count up from 1 infinitely.

I made this model but it doesn’t work because the loop always resets the variable onTimer to 1 then add’s 1 making the output say, “2, 2, 2, 2, 2, 2, etc”

[COLOR=dimgray]var timer:Timer = new Timer(1000);[/COLOR]
[COLOR=dimgray]timer.addEventListener(TimerEvent.TIMER, onTimer);[/COLOR]
[COLOR=dimgray]timer.start();[/COLOR]

[COLOR=dimgray]function onTimer(e:TimerEvent):void [/COLOR]
[COLOR=dimgray]{[/COLOR]
[COLOR=dimgray]var now:Number = (1); now++; [/COLOR]
[COLOR=dimgray]trace(now);[/COLOR]
[COLOR=dimgray]}[/COLOR]

[COLOR=dimgray][COLOR=black]I know[/COLOR] [/COLOR]I need to create the now variable outside the onTimer because this is accessed every second. So I tried this but it doesn’t work and I’m starting to get really confused.

[COLOR=dimgray]var timer:Timer = new Timer(1000);[/COLOR]
[COLOR=dimgray]timer.addEventListener(TimerEvent.TIMER, onTimer);[/COLOR]
[COLOR=dimgray]timer.start();[/COLOR]

[COLOR=dimgray]function onTimer(e:TimerEvent):void [/COLOR]
[COLOR=dimgray]{[/COLOR]
[COLOR=dimgray]var then:Number = (0);[/COLOR]
[COLOR=dimgray]then.addEventListener(TimerEvent.TIMER, addTo);[/COLOR]
[COLOR=dimgray]}[/COLOR]

[COLOR=dimgray]function addTo(e:TimerEvent):void[/COLOR]
[COLOR=dimgray]{[/COLOR]
[COLOR=dimgray]var now:Number = then++;[/COLOR]
[COLOR=dimgray]trace(now)[/COLOR]
[COLOR=dimgray]}[/COLOR]

Thanks for your help!

You need to ask yourself what use it is resetting the now value to 1 every time the Timer event fires…



var timer:Timer = new Timer(1000);
timer.addEventListener(TimerEvent.TIMER, onTimer);
timer.start();

var now:int = 0;
function onTimer(e:TimerEvent):void 
{
    now++; 
    trace(now);
}

Notice it’s outside of the function call (and an integer, given it’ll only be displaying integer data).

Oh I see!
How wonderful thank you.
I’m just really new to how the syntax reads out. Tought me a good lesson, thank you McGuffin.

So I want to do the same thing only on Enter Frame instead of using a timer. I must just be missing something…

 
//stage.addEventListener(Event.ENTER_FRAME,onFrameLoop);
//
//var now:Number = (0);
//
//function onFrameLoop(evt:Event):void
//{
// (now++);
// if (now == (now %= 12))
// trace(now);
//}
 
stage.addEventListener(Event.ENTER_FRAME,onFrameLoop);
var now:Number = (a++);
var a:uint;
function onFrameLoop(evt:Event):void
{
(now++);
if (a == (now %= 12))
trace(a);
}

I’ve included two attempts, both with interesting outcomes.

what are you trying to do with that?

on the first one you are making now increase by 1, but in the if statement, you are changing the value of now again with %= (thats like doing now = now % 12) and since now is that number its like checking now == now. so it will keep tracing now every frame.

on the second you are again changing now with the if but you are checking if a == now. somehow a equals 1 so when now cycles around to 1 it traces a which is 1. so about every 12 frames it traces 1;

I am trying to make the now variable add 1 every frame to the count and then every 12 time the count hits a number that is divisible by 12 it traces 1, 2, 3, 4, 5, 6, 7, 8 etc. Basically I want the numbers in the output to go up by 1 incrementally every second just like how we got it to work in this thread previously with a Timer, instead of Enter_Frame.

Doesn’t the notation of " %= " mean divide? I don’t know I’m a little confused.

% is modulo, it divides and returns the Remainder, meaning that ‘now % 12’ will return 0 is the number is divisible by 12. %= is different in that it will mod itself then assign itself the returned value, like i said before, now %= 12 is like using now = now % 12.

if you want stuff to happen it a certain time frame like a second i suggest use getTimer() which returns how long the program has been running in milliseconds.

var lastTime:Number = 0;
var currentTime:Number = 0;

stage.addEventListener(Event.ENTER_FRAME, onFrameLoop);
function onFrameLoop(e:Event) {
	
	currentTime = getTimer() / 1000  // turn into seconds
	
	if (currentTime - lastTime > 1) {
		lastTime = currentTime;
		
		// stuff every second here
	}
	
}