PDA

View Full Version : Why doesn't this work? (Average script)



babywax
August 29th, 2003, 07:02 PM
I can't figure out why this won't work...
I'm trying to make a new Math function, called Math.halfway
It adds two variables together and returns half of their value.
The problem is, when I use it, on a value of 100 and 200, it comes up with 159.5? Why does it do this? Thank you for any help, the flash file is attached.

babywax
August 29th, 2003, 07:30 PM
Ok, I did a little testing with it and found out that the result is always 9.5 higher than it should be. This is easily patchable, but why does it not work in the first place?


Math.halfway = function (numOne, numTwo) {
this.numOne = numOne;
this.numTwo = numTwo;
var num = this.numOne + this.numTwo;
return num/2;
delete this.numOne;
delete this.numTwo;
}

there's the script.

babywax
August 29th, 2003, 07:32 PM
Sorry to spam, but I found out the symbol I was using was 9.5 off... I know, pretty stupid :sigh:

jingman
August 30th, 2003, 11:23 AM
just wanted to offer you a little help with your function (cleaning it).

Also, adding two numbers and getting the halfway point between them is just the average of the two numbers, so try something like this.

Math.average = function (numOne, numTwo) {
return (numOne+numTwo)/2;
}

Haven't tested it, but that should return the same result. When you are passing variables to the function, just do it like this:

Math.average(this.numOne, this.numTwo)

kode
August 30th, 2003, 11:37 AM
Just in case you need the average of more than two numbers...
Math.average = function() {
var i, n;
for (i=0; i<arguments.length; i++) n += arguments[i];
return n/arguments.length;
};

ericlin
August 30th, 2003, 12:17 PM
Hi babywax

Open the info panel (not property panel). There is an icon of 9 small boxes. Usually the left upper one is selected. You can change it to select the central one.

Then the _x will be shown correctly in property panel.