average velocity, as3
I need something accurate I can plug equations in to if you can help. How would you apply the equation bellow? Thanks guys.
AVERAGE VELOCITY AND DISPLACEMENT
average velocity V=X/Tdisplacement
x=v*Tmore inf开发者_运维百科o
example
I have 30 seconds and a field that is 170 yards. What average velocity would I need my horse to travel at to reach the end of the field in 30 seconds. I moved the decimal places around and got this.
alt text http://www.ashcraftband.com/myspace/videodnd/VQ.jpg
Here's what I tried 'the return value is close, but not close enough'
FLA herevar TIMER:int = 10;
var T:int = 0;
var V:int = 5.6;
var X:int = 0;
var Xf:int = 17000/10*2;
var timer:Timer = new Timer(TIMER,Xf);
timer.addEventListener(TimerEvent.TIMER, incrementCounter);
timer.start();
function formatCount(i:int):String {
var fraction:int = Math.abs(i % 100);
var whole:int = Math.abs(i / 100);
return ("0000000" + whole).substr(-7, 7) + "." + (fraction < 10 ? "0" + fraction : fraction);
}
function incrementCounter(event:TimerEvent) {
T++;
X = Math.abs(V*T);
text.text = formatCount(X);
}
tests
TARGET
5.6yards * 30seconds = 168yardsINTEGERS
135.00 in 30 secondsMATH.ROUND
135.00 in 30 secondsNUMBERS
140.00 in 30 secondscontrol timer 'I tested with this and the clock on my desk'
var timetest:Timer = new Timer(1000,30);
var Dplus:int = 17000;
timetest.addEventListener(TimerEvent.TIMER, cow);
timetest.start();
function cow(evt:TimerEvent):void {
tx.text = String("30 SECONDS: " + timetest.currentCount);
if(timetest.currentCount> Dplus){
timetest.stop();
}
}
//far as I got...couldn't get delta to work...
T = (V*timer.currentCount);
X += Math.round(T);
i think your problem is that you suppose that passed time = delay*count. That is not true. Timers on modern OSes are not precise, this is because many threads concur for CPU and time comes in fixed intervals, this is especially noticeable if timer interval is small.
So how to this? You should take actual time spans that happened between two ticks of timer, that will give you the time delta. Then x += v*deltaT
.
Example:
function incrementCounter(event:TimerEvent) {
var now:int = new Date().getTime();
X += Math.abs(V*(now - T));
text.text = formatCount(X);
T = now;
}
精彩评论