-1

This is probably basic math that I don't seem to remember.

I'm trying to get from 0 to 5,000,000 in 10 seconds while having all the numbers ticking. I don't have to have the number reach exactly 5,000,000 because I can just do a conditional for it to stop when it's over.

Right now I have this:

count+= 123456

if (count > 5000000) {
    count = 5000000;
}

It gives the sense of number moving you know? But It really starts off too high. I wanted to gradually climb up.

4

2 回答 2

5

你可以这样做:

function timedCounter(finalValue, seconds, callback){

  var startTime = (new Date).getTime();
  var milliseconds = seconds*1000;

  (function update(){

    var currentTime = (new Date).getTime();
    var value = finalValue*(currentTime - startTime)/milliseconds;

    if(value >= finalValue)
      value = finalValue;
    else
      setTimeout(update, 0);

    callback && callback(value);

  })();

}

timedCounter(5000000, 10, function(value){
  // Do something with value
});

演示

请注意,数字大到5000000您不会看到最后几个数字发生变化。您只会看到像5000. 你可以解决这个问题;也许通过添加一些随机性:

value += Math.floor(Math.random()*(finalValue/10000 + 1));

带有随机性的演示

于 2013-08-21T21:40:41.263 回答
2

你可以补间:

import fl.transitions.Tween;
import fl.transitions.easing.Regular;

var count = 0;
var tween:Tween = new Tween(this, "count", Regular.easeInOut,0,5000000,10, true);

这将在 10 秒内将变量计数从 0 变为 5000000。如果您想扩展此代码,请阅读这些类。

祝你好运!

于 2013-08-22T21:41:43.067 回答