I’m writing a very simple function where I am supposed to find the distance between two 3 d points. One set of points are given directly as floats, whereas the others are given as a horizontal array.
The three inputs are x,y,z which are the floats and a row pickups[0] being passed as par which is [“blue1”,441.2223, 231.44, 127.7466]
Now, the row comes up as an object of course. I was having a problem with making a function to calculate the distance: My code as of now is:\
function distance(x,y,z,par)
{
var p:float;
p=Mathf.Sqrt( (x-parseFloat(par[1].ToString())) * (x-parseFloat(par[1].ToString())) + (y-parseFloat(par[2].ToString())) * (y-parseFloat(par[2].ToString())) + (z-parseFloat(par[3].ToString())) * (z-parseFloat(par[3].ToString())));
return p;
}
Please try and help me out.
The error has to do with
var p:float;, you don’t need to try and predeclare the variable p as anything as javascript is type-less. What the reference is refering to withstatic function Sqrt (f : float) : floatis that it is expecting a variable that is a float and returns a float. Your code should be written as:You also don’t need toString() your parameters as they will be parsed into a float.