# Specifying which array to filter through in function

**URL:** <https://forum.kirupa.com/t/specifying-which-array-to-filter-through-in-function/208519>\
**Category:** Uncategorized\
**Created:** [November 29, 2006, 4:40pm UTC](https://forum.kirupa.com/t/specifying-which-array-to-filter-through-in-function/208519 "2006-11-29T16:40:14Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![big\_britches](https://avatars.discourse-cdn.com/v4/letter/b/d26b3c/32.png) [@big\_britches](https://forum.kirupa.com/u/big_britches)\
**Post date:** [November 29, 2006, 4:40pm UTC](https://forum.kirupa.com/t/specifying-which-array-to-filter-through-in-function/208519/1 "2006-11-29T16:40:14Z")

</div>

Here’s a link to my project, to give an idea of what’s happening:

[http://deliver.capstrat.com/development/capstrat/banner/index.html](http://deliver.capstrat.com/development/capstrat/banner/index.html)

I’ve had trouble in the past getting any direction or answers here - I want to specify that I’m not asking for explicit help; I know we’re all busy. I’m just wanting general, I’m-on-the-right-track-and-yes-this-is-possible direction…or conversely, dude-you’re-all-wrong-and-you-should-go-read-this-article-or-this-section-of-Flash-Help.  
Thanks in advance…

I have a single function, which is to randomly place mc’s on stage. These mc’s are found in one of 3 arrays. To avoid overlap when placing the mc on the stage, I have a test set up within this function - the x-position randomly generated is to be checked against the x-positions of all other mc’s in that particular array; if overlap is found, then the generated x-position is thrown out and another generated, until an acceptable one is found.

Because I have 3 separate arrays of mc’s to pull from to populate the stage, I originally had 3 separate functions to call. But that seemed redundant, as each of the three needs to do the exact same thing. So now I’ve consolidated into one function, and am missing something very important, conceptually. My question is this: is it possible to have a variable as a parameter in my function call, which I can point to whichever of the 3 established arrays I want the function to look at? Here’s the line I’m referring to:

function getXposition(placement, **objectType** ) {

where I’m thinking that objectType can be the reference to the array I want the function to focus on for that particular call. I’m using a for loop to cycle through each item in the array that I’m looking at:

for (var item in **objectType** ) {

where previously I would have said

for (var item in **directorsArray** ) {

for example. Here’s the function as is, followed by the onclip event script:

XXXXXXXXXXXXXXXXXXXXX  
ARRAYS  
XXXXXXXXXXXXXXXXXXXXX  
\_root.objectsArray = new Array();  
\_root.objectsArray = [“squirrel\_mc”, “lava\_mc”, “flag\_mc”, “rocket\_mc”, “cone\_mc”, “boomerang\_mc”];  
\_root.directorsArray = new Array();  
\_root.directorsArray = [“stephen\_mc”, “lisa\_mc”, “dchatham\_mc”, “billy\_mc”, “pmahoney\_mc”, “sballew\_mc”, “frank\_mc”, “jodi\_mc”];  
\_root.leadershipArray = new Array();  
\_root.leadershipArray = [“james\_mc”, “debbie\_mc”, “johnp\_mc”, “todd\_mc”, “karen1\_mc”, “ken\_mc”];

XXXXXXXXXXXXXXXXXXXX  
FUNCTION  
XXXXXXXXXXXXXXXXXXXX

function getXposition(placement, **objectType** ) {  
keepTesting = true;  
var monkeyspunk:Array = new Array(); //make array  
for (var item in objectType) { //loop thru hard coded array  
if (objectType == “objectsArray”) {  
if (object.\_x\>(0-\_root.foregroundWidth) && object.\_x\<\_root.foregroundWidth) { //if object is on stage  
monkeyspunk.push = newXPos; //add it’s x pos to array  
}  
} else if (objectType == “directorsArray”) {  
if (director.\_x\>(0-\_root.foregroundWidth) && director.\_x\<\_root.foregroundWidth) { //if object is on stage  
monkeyspunk.push = newXPos; //add it’s x pos to array  
}  
} else if (objectType == \_root.leadershipArray) {  
if (leader.\_x\>(0-\_root.foregroundWidth) && leader.\_x\<\_root.foregroundWidth) { //if object is on stage  
monkeyspunk.push = newXPos; //add it’s x pos to array  
}  
}  
while (keepTesting == true) {  
//test  
if (placement == “init”) {  
newXPos = (Math.random()_(2_\_root.foregroundWidth))-\_root.foregroundWidth;  
//random formula;  
} else if (placement == “left”) {  
newXPos = 0-Math.random()_\_root.foregroundWidth;  
//random formula;  
} else if (placement == “right”) {  
newXPos = \_root.stageWidth+Math.random()_(\_root.foregroundWidth-\_root.stageWidth);  
//random formula;  
}  
//random formula;  
newXPosValid = true;  
for (var item in monkeyspunk) {  
//test  
spaceLeft = item-100;  
spaceRight = item+100;  
if (newXPos\>spaceLeft && newXPos\<spaceRight) {  
newXPosValid = false;  
}  
}  
//can we escape the while loop?  
if (newXPosValid == true) {  
keepTesting = false;  
}  
}  
}  
return newXPos;  
}  
stop();

XXXXXXXXXXXXXXXXX  
THE ON CLIP EVENT ACTIONS  
XXXXXXXXXXXXXXXXX

onClipEvent (load) {  
// add all the objects at random, from -X to X  
for (i=0; i\<\_root.objectsArray.length; i++) {  
object = this.attachMovie(_root.objectsArray\*, "object_"+i, 100+i);  
object.\_y = Math.random()_((\_root.stageHeight-object.\_height)+30);  
object.\_x = getXposition(“init”, “objectsArray”);  
// set a depth for parallax effect  
object.\_xscale = object.\_yscale=\_root.objectMinScale+(Math.random()_(\_root.objectMaxScale-\_root.objectMinScale));  
}  
// add all the directors at random, from -X to X  
for (i=0; i\<\_root.directorsArray.length; i++) {  
director = this.attachMovie(_root.directorsArray\*, "director_"+i, 300+i);  
director.\_xscale = director.\_yscale=_root.directorMinScale+(Math.random()(\_root.directorMaxScale-\_root.directorMinScale));  
director.\_y = (\_root.stageHeight-director.\_height);  
director.\_x = getXposition.apply(“init”, directorsArray);  
}  
for (i=0; i\<\_root.leadershipArray.length; i++) {  
leader = this.attachMovie(\_root.leadershipArray, "leader_"+i, 350+i);  
leader.\_y = (\_root.stageHeight-leader.\_height);  
leader.\_x = getXposition();  
}  
}  
onClipEvent (enterFrame) {  
// move each object at a rate relative to their parallax  
for (i=0; i\<_root.objectsArray.length; i++) {  
object = eval("this.object_"+i);  
object.\_x += 0.75\*(\_root.xVelocity+\_root.xVelocity\*((object.\_xscale-\_root.objectMinScale)/\_root.objectMaxScale));  
if (object.\_x\>\_root.foregroundWidth) {  
object.\_x = 0-Math.random()_\_root.foregroundWidth;  
object.\_y = Math.random()_((\_root.stageHeight-object.\_height)+30);  
} else if (object.\_x\<0-\_root.foregroundWidth) {  
object.\_x = \_root.stageWidth+Math.random()_(\_root.foregroundWidth-\_root.stageWidth);  
object.\_y = Math.random()_((\_root.stageHeight-object.\_height)+30);  
}  
}  
// move each object at a rate relative to their parallax  
for (i=0; i\<_root.directorsArray.length; i++) {  
director = eval("this.director_"+i);  
director.\_x += \_root.xVelocity+\_root.xVelocity\*((director.\_xscale-\_root.directorMinScale)/\_root.directorMaxScale);  
if (director.\_x\>\_root.foregroundWidth) {  
director.\_x = getXposition(“left”);  
//director.\_x = 0 - Math.random() \* \_root.foregroundWidth;  
} else if (director.\_x\<0-\_root.foregroundWidth) {  
director.\_x = getXposition(“right”);  
//director.\_x = \_root.stageWidth+Math.random()_(\_root.foregroundWidth-\_root.stageWidth);  
}  
}  
// if the object is beyond x or -x, send it off stage, somewhere random  
for (i=0; i\<root.leadershipArray.length; i++) {  
leader = eval("this.leader"+i);  
leader.\_x += 1.5_\_root.xVelocity;  
if (leader.\_x\>\_root.foregroundWidth) {  
leader.\_x = 0-Math.random()_\_root.foregroundWidth;  
} else if (leader.\_x\<0-\_root.foregroundWidth) {  
leader.\_x = \_root.stageWidth+Math.random()_(\_root.foregroundWidth-\_root.stageWidth);  
}  
}  
}

**NOTE - monkeyspunk is the name of a temporary array I’m creating to populate with valid x-positions; it’s scope is limited to the function itself. It gets tossed out at the end of the function**
