View Full Version : flash doesn't add numbers right... help!!!
ovi dio
July 15th, 2005, 10:34 PM
Hi everybody! Thanks in advance for your help!
Here's my problem:
sometimes when within an onEnterFrame, when I add var1+=var2 flash doesn't do it's Math the right way. It sort of says 2.15+2.45=4.55 I'm using trace() to check on the values and that is exactly what it does.
What's the explanation for this and how can it be fixed?
Thanks!
nathan99
July 15th, 2005, 10:37 PM
try something like
Number(var1)+=Number(var2)
ovi dio
July 15th, 2005, 10:41 PM
the thing is that the vars are already declared as
var variable:Number;
I'm going to give it a try still, hopefully it'll work
nathan99
July 15th, 2005, 11:00 PM
this goes alright
Seb Hughes
July 16th, 2005, 06:14 AM
Here i made it even better, i restricted character entry in the box, so people can only enter numbers.
ovi dio
July 16th, 2005, 01:14 PM
Here's a sample code that shows exactly what the problem is.
Just copy the code in the first frame of an empty fla and test it. Watch the output window and take a look at the values:
//----------------------start code--------------------//
var totalDist:Number = 90;//total distance to be traveled
var time:Number = 1;//the total time of the animation= 1 second
var frameRate:Number=50;
var step:Number=1;
var iVit:Number = 2*totalDist/time;//viteza initiala
//viteza finala vreau sa fie 0
var acc:Number = -2*totalDist/(time*time);//the acceleration or better said deceleration
var prevDist:Number=0;
//-----------------------------------
function calcDist():Number {
var dist:Number;//distance to be calculated
var tInt:Number = step*time/frameRate; //the time interval
trace( "time passed: " + tInt);
//calculating "dist"
//this is the kinematic formula for distance considering final speed and initial distance to be 0
//I'm using 0.05*Math.round(var/0.05) because this value is going to be used to animate a movie clip
//and flash can only move things on screen with a 0.05 precision
dist = 0.05*Math.floor((iVit*tInt+acc*tInt*tInt/2)/0.05);
var increase:Number=dist-prevDist;
trace("previous distance is: " + prevDist);
trace("distance now is: " + dist);
trace("distance now - previous distance is increase: " + increase);
prevDist=dist;
step++;
return dist;
}
//-----------------------------------------------
_root.onEnterFrame=function() {
var myDist:Number=calcDist();
//trace("distance is now: " + myDist);
trace("-----------------------------");
if(myDist>=90) {
delete _root.onEnterFrame;
}
};
//----------end code ---------------------//
ovi dio
July 16th, 2005, 01:20 PM
you'll have to add the double quotes in the trace()
it looks like when I copy/paste the script those got messed up
ovi dio
July 16th, 2005, 01:26 PM
Here's a link to the as file:
http://www.ovispack.com/Script-1_test.as
ovi dio
July 17th, 2005, 09:48 AM
anybody has an explanation to this problem?
Please!!! It's driving me nuts :)
ovi dio
July 17th, 2005, 10:17 AM
here's a more simple example of the problem:
-it's a simple subtraction and yet, after a few iteration, flash goes crazy
Why does it do that?
Just paste the code on the first frame of an empty fla and tate it. You'll see the problem in the output panel.
//sample code
var const:Number=10;
var no1:Number;
for(i=1; i<100; i++) {
no1=const- 0.15*i;
trace("no1 is: " + no1);
}
//end sample code
coolname
July 17th, 2005, 10:44 AM
I ran through that entire .as file, and I didn't see any subtraction errors. Could you give an example of what you mean?
virusescu
July 17th, 2005, 11:11 AM
Read this
http://www.macromedia.com/cfusion/knowledgebase/index.cfm?id=tn_13989
;)
ovi dio
July 17th, 2005, 01:24 PM
virusescu, mersi mult pt link :)
Thanks everybody! It looks like macromedia has already given an explanation for what's happening :)
Coolname, if you'll look closer at some of the values in the output panel you'll see what I was talking about
Thanks
virusescu
July 17th, 2005, 01:32 PM
he he :beer: - cu placere. New York? Heh, te invidiez :)
It looks like macromedia has already given an explanation for what's happening
If you read carefully the link at macromedia you'll notice that it's not like "what's happening" is because of macromedia's fault. It's just the way things (floating point numbers) are.
ovi dio
July 17th, 2005, 01:38 PM
virusescu, nu ai de ce sa ma invidiezi :)
o sa ma intorc in romania si sper sa fiu mai fericit acolo :) ... nu ca aici as fi nefericit doar ca din anumite puncte de vedere nu e la fel
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.