Copy link to clipboard
Copied
I'm creating an animated info graphic about tigers and need a count up from 0 to 3200 to show the current population of tigers in Asia.
This is my current Code:
import flash.display.*;
import flash.events.*;
import flash.utils.*;
var countUpInc:int = 20;
var totalSecs:int = 1000;
var count = 0;
var countTimer:Timer = new Timer(countUpInc);
countTimer.addEventListener(TimerEvent.TIMER, timerHandler);
countTimer.start();
function timerHandler(e:TimerEvent): void {
if (count !== 3200) {
count = count + countUpInc;
counter.text = count;
trace(count);
}
}
the code runs in console but when line 19 is uncommented it crashes at 1280.
I get the error:
" TypeError: Error #1009: Cannot access a property or method of a null object reference.
at TigerInfographic_fla::MainTimeline/timerHandler()[TigerInfographic_fla.MainTimeline::fram e210:17]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick() "
i don't know how to fix it.
.fla file: NBeaulieu_FinalAnimation.zip - Google Drive
Copy link to clipboard
Copied
Hi.
I spotted a few problems:
- The critical one is that you didn't put a stop() function in the frame where your code are so the timeline continues playing until it goes back to the beginning and don't find the text field anymore.
- Also, there are two labes named 'counter'. Remove one of them.
- You have to embed the font you are using in the Properties Panel > Embed...
- Your counter text field and its parent Movie Clip are oddly scaled. Try to always keep things at a 100% if you don't need to tween them.
Alternatively, I suggest you to use a tween approch because it's easier to set the total time and you can even use different easing functions.
import fl.transitions.Tween;
import fl.motion.easing.*;
import fl.transitions.TweenEvent;
var count:int = 0;
var tween:Tween = new Tween(this, "count", Sine.easeOut, 0, 3200, 10, true);
tween.addEventListener(TweenEvent.MOTION_CHANGE, function():void
{
counter.countertext.text = String(count);
});
stop();
Regards,
JC