PDA

View Full Version : Getting nearest value from an array (arrays again...)



ilyaslamasse
February 7th, 2002, 09:44 AM
I have an array set up with a list of values which are actualy button positions in a movie clip......I know how to get the first or last values of the array, but what I need help on is creating a code to get the _x position of the clip and then access the array list to jump to the nearest button position in the movie clip......I hope I'm making sence.....

Thanx :)

suprabeener
February 7th, 2002, 11:59 PM
Array.prototype.getnearestto = function(v){

var n,r,diff,t;

n = r = this.length-1;

diff = Math.abs(v-this[n]);

while(n--){

t = Math.abs(v-this[n]);

if(t<diff){ r=n; diff=t; }

}

return(this[r]);

}


usage:


values = [37,5,8,87,324,6,36,43,59];

test = values.getnearestto(100); // test = 87;
i wasn't quite sure what you wanted, but this you should be able to modify this to do what you want.

me
February 11th, 2002, 10:34 AM
Here is the code I am attempting to use:.....

&nbsp &nbsp &nbsp &nbsp pos = new Array("2", "102", "208", "308", "410", "514", "620", "726", "830", "934");

&nbsp &nbsp &nbsp &nbsp z = getProperty("movieclip", _x);
&nbsp &nbsp &nbsp &nbsp
do {
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp a = a+1;
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp y = (pos[a]);
&nbsp &nbsp &nbsp &nbsp } while (y<=z);
&nbsp &nbsp &nbsp &nbsp
setProperty ("movieclip", _x, -y);

suprabeener
February 11th, 2002, 03:55 PM
make sure that you define getnearestto before running the code, it only needs to be defined once to exist.


movieclip._x = pos.getnearesto(movieclip._x);

help
February 12th, 2002, 10:18 AM
I still cant get the code to work :( !

Here's the code I'm using
on (rollOver) {
&nbsp &nbsp &nbsp &nbsp getnearestto = function (v) { var n, r, diff, t;n = r=this.length-1;diff = Math.abs(v-this[n]);while (n--) {t = Math.abs(v-this[n]);if (t<diff) {r = n;diff = t;}}};
&nbsp &nbsp &nbsp &nbsp tellTarget ("striponstage") {
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp gotoAndPlay (5);
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp pos = new Array("2", "102", "208", "308", "410", "514", "620", "726", "830", "934", "1040", "1144", "1248", "1354", "1458", "1562", "1666", "1768", "1872", "1980", "2084", "2186", "2292");
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp clipposit = getProperty("striponstage.strip", _x);
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp z = pos.getnearesto(clipposit);
&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp setProperty ("striponstage.strip", _x, -(z));
&nbsp &nbsp &nbsp &nbsp }
}

ilyaslamasse
February 12th, 2002, 02:11 PM
Well, one thing wrong for sure is that you define your function everytime you rollover the button. Put that code somewhere else in the clip, in a frame or something. I think that this is not why your clip is acting wrong, but it's a start.

pom 0]

suprabeener
February 12th, 2002, 02:22 PM
i think it's returning zero, not the first array item.

you seem to have changed the prototype i put in my first post. it's not returning anything anymore and you've removed the Array.prototype from the first line, it's important to leave those in. there's no need to define it every rollover, just once will suffice.

cut and paste the definition in my first post into somewhere at the beginning of your movie where it will only run once. don't forget to include the Array.prototype bit.

don't put quotes around your numbers in the array, that's making them strings and they'll work better as numbers.

otherwise, looks good! should work.

ilyaslamasse
June 2nd, 2002, 11:32 AM
Just for the record :
But here's something that should work :

Array.prototype.getMinimum = function(){

var n,min,i;

n = this.length-1;

min = this[0 ];

for (i=0;i<n;i++) {

&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp if (this[i ]<min)

&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp min = this[i ];

}

return(min);

}



values = [37,5,8,87,-5,324,6,36,43,59];

test = values.getMinimum();

trace (test) ;

pom 0]