Getting nearest value from an array (arrays again...)

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…\r\rThanx :slight_smile:

 \r\rArray.prototype.getnearestto = function(v){\r\r   var n,r,diff,t;\r\r   n = r = this.length-1;\r\r   diff = Math.abs(v-this[n]);\r\r   while(n--){\r\r      t = Math.abs(v-this[n]);\r\r      if(t<diff){ r=n; diff=t; }\r\r   }\r\r   return(this[r]);\r\r}\r\r

\rusage:

 \r\rvalues = [37,5,8,87,324,6,36,43,59];\r\rtest = values.getnearestto(100); // test = 87;

\ri wasn’t quite sure what you wanted, but this you should be able to modify this to do what you want.

Here is the code I am attempting to use:…\r\r&nbsp &nbsp &nbsp &nbsp pos = new Array(“2”, “102”, “208”, “308”, “410”, “514”, “620”, “726”, “830”, “934”);\r\r&nbsp &nbsp &nbsp &nbsp z = getProperty(“movieclip”, _x);\r&nbsp &nbsp &nbsp &nbsp \rdo {\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp a = a+1;\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp y = (pos[a]);\r&nbsp &nbsp &nbsp &nbsp } while (y<=z);\r&nbsp &nbsp &nbsp &nbsp \rsetProperty (“movieclip”, _x, -y);

make sure that you define getnearestto before running the code, it only needs to be defined once to exist.\r

 \r\rmovieclip._x = pos.getnearesto(movieclip._x);

I still cant get the code to work :frowning: !\r\rHere’s the code I’m using\ron (rollOver) {\r&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;}}};\r&nbsp &nbsp &nbsp &nbsp tellTarget (“striponstage”) {\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp gotoAndPlay (5);\r&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”);\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp clipposit = getProperty(“striponstage.strip”, _x);\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp z = pos.getnearesto(clipposit);\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp setProperty (“striponstage.strip”, _x, -(z));\r&nbsp &nbsp &nbsp &nbsp }\r}

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.\r\rpom 0]

i think it’s returning zero, not the first array item.\r\ryou 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.\r\rcut 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.\r\rdon’t put quotes around your numbers in the array, that’s making them strings and they’ll work better as numbers.\r\rotherwise, looks good! should work.

Just for the record :

 But here's something that should work :\r\rArray.prototype.getMinimum = function(){\r\r   var n,min,i;\r\r   n = this.length-1;\r\r   min = this[0 ];\r\r   for (i=0;i<n;i++) {\r\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp if (this[i ]<min)\r\r&nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp &nbsp min = this[i ];\r\r   }\r\r   return(min);\r\r}\r\r\r\rvalues = [37,5,8,87,-5,324,6,36,43,59];\r\rtest = values.getMinimum();\r\rtrace (test) ;

\r\rpom 0]