开发者

Flash: simultaneously changing numbers?

How do I create a simultaneously changing number in flash? Something along the lines of milliseconds on a stopwatch, like how they ch开发者_运维技巧ange repeditively.

Thanks in advance!


read through this, it will give you all the info you need: http://www.republicofcode.com/tutorials/flash/as3timer/

but essentially you just need something like this

var myTimer:Timer = new Timer(10,0);
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
myTimer.start();

function timerListener (e:TimerEvent):void{
  trace("10 ms passed");
}

the timer is not precise (so you will get +/- a few milliseconds here or there depending on the load, so if you want to use it to display time, use getTimer() http://www.adobe.com/livedocs/flash/9.0/ActionScriptLangRefV3/flash/utils/package.html


millisecond precision would be pretty hard to create this way, and there is no point in updating the timer more often than the frame-rate, as the user can't see the result. (unless there's you're doing more than displaying the time)

but you could have your code look something like this

var myTimer:Timer = new Timer(10,0);
var startTime:int = -getTimer();
myTimer.addEventListener(TimerEvent.TIMER, timerListener);
myTimer.start();

function timerListener (e:TimerEvent):void{
  someTextfield.text = ("ms passed: " + getTimer()+startTime );
}

which would show you on someTextfield the number of ms passed;


One way to do this is to change the number each frame. To do this, you can use the Enter Frame event. Here is an example.

var frameCount:int = 0;

// Add an event listener that will fire on each ENTER_FRAME event.
addEventListener(Event.ENTER_FRAME, OnEnterFrame);

//This function will get called after each frame
function OnEnterFrame(e:Event):void{
    frameCount++;
    trace(frameCount);
}

This example will trace the frameCount once per frame (several times a second). If you wanted to instead keep track of time, you would need to do 2 things.

  1. Declare a variable for when the timer starts, and set it to the current time, which can be done like this:

    var startTime:Number = (new Date()).time;

  2. Inside the OnEnterFrame function, grab the new time using the same (new Date()).time method, and subtract the start time from it. This will give you the diference in milliseconds between when the timer started, and what the current time is.

Events are a big part of AS3, here is a tutorial if you want to learn more. Hope this helped!


if you want this for a matrix like effect, you'll probably be better off creating a movieclip. There is no point to doing extra calculations, and having a bunch of event listeners updating a text field above the framerate is overkill.

Add a vertical blur filter on the numbers (or overlay another number with a lowered opacity) and have them skip numbers. That will make it look like it's being updated quickly, because just changing the numbers won't have that effect.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜