Groups | Blog | Home
all groups > flash actionscript > october 2006 >

flash actionscript : hitTest for array of nested MovieClips


adam NO[at]SPAM blueapplestudio
10/10/2006 9:54:31 PM
Been working on this on and off throughout the day, and am having no luck.

On the stage are 3 instances of a Target MovieClip. Through scripting, an
empty MovieClip is created. Then, MCs (Pentagon, Square, and Triangle) are
added from the library into the empty MC. At this time, an array is made
listing the instance names of these MCs.

I would like to use this array (aInstanceNamesInMCContainer) to perform a hit
test against the Target MCs, but I?m getting hung up on the syntax. The
problem is that I am not properly targeting the nested MC, but I have been
unable to figure out the solution.

Any help is appreciated.


//Defines a string.
var sShapeList:String = "Pentagon,Pentagon,Square,Triangle,Square,Pentagon";

//Creates a string from the string sShapeList.
var aShapeList:Array = sShapeList.split(",");

//Creates an empty MovieClip, and sets its position.
this.createEmptyMovieClip("mcContainer", this.getNextHighestDepth());
mcContainer._y = 230;

//Creates an empty array that will hold the instance names of the MCs found in
mcContainer.
var aInstanceNamesInMCContainer:Array = [];

//Attaches MovieClips from the library into mcContainer.
for (i = 0; i < aShapeList.length; i++){
mcContainer.attachMovie(aShapeList[i], aShapeList[i]+i,
mcContainer.getNextHighestDepth(), {_x: i * 95});
aInstanceNamesInMCContainer.push(aShapeList[i]+i);
}

//(Theoretically) Performs a hitTest against the 1st Target MC and the MCs
within mcContainer.
for (i = 0; i < aInstanceNamesInMCContainer.length; i++){
trace(mcTarget1.hitTest("mcContainer." +
String(aInstanceNamesInMCContainer[i])));
trace("mcContainer." + String(aInstanceNamesInMCContainer[i]));
}
coldMiner
10/10/2006 10:18:19 PM
Try this:

//Defines a string.
var sShapeList:String = "Pentagon,Pentagon,Square,Triangle,Square,Pentagon";

//Creates a string from the string sShapeList.
var aShapeList:Array = sShapeList.split(",");

//Creates an empty MovieClip, and sets its position.
this.createEmptyMovieClip("mcContainer", this.getNextHighestDepth());
mcContainer._y = 230;

//Creates an empty array that will hold the instance names of the MCs found in
mcContainer.
var aInstanceNamesInMCContainer:Array = [];

//Attaches MovieClips from the library into mcContainer.
for (i = 0; i < aShapeList.length; i++){
var mmc:MovieClip;
mmc = mcContainer.attachMovie(aShapeList[i], aShapeList[i]+i,
mcContainer.getNextHighestDepth(), {_x: i * 95});
aInstanceNamesInMCContainer.push(mmc);
}

//(Theoretically) Performs a hitTest against the 1st Target MC and the MCs
within mcContainer.
for (i = 1; i < aInstanceNamesInMCContainer.length; i++){
trace(aInstanceNamesInMCContainer[0].hitTest(aInstanceNamesInMCContainer[i]));
trace(aInstanceNamesInMCContainer[i]);
}
AnandMX
10/11/2006 12:00:00 AM
Your code is not working at my place,still try this code, i hope it will work.
//Defines a string.
var sShapeList:String = "Pentagon,Pentagon,Square,Triangle,Square,Pentagon";

//Creates a string from the string sShapeList.
var aShapeList:Array = sShapeList.split(",");

//Creates an empty MovieClip, and sets its position.
this.createEmptyMovieClip("mcContainer", this.getNextHighestDepth());
mcContainer._y = 230;

//Creates an empty array that will hold the instance names of the MCs found in
mcContainer.
var aInstanceNamesInMCContainer:Array = [];

//Attaches MovieClips from the library into mcContainer.
for (i = 0; i < aShapeList.length; i++){
var tmpMov:MovieClip= mcContainer.attachMovie(aShapeList[i], aShapeList[i]+i,
mcContainer.getNextHighestDepth(), {_x: i * 95});
aInstanceNamesInMCContainer.push(aShapeList[i]+i);
if (tmpMov.hitTest(mcTarget1)){
trace("mcContainer." + this);
}

}
adam NO[at]SPAM blueapplestudio
10/11/2006 4:00:21 AM
I pasted your code and it doesn't seem to work; perhaps I am missing something.
I believe I tried something similar where I used a variable that represented
the string value I need, and I had no success.

The .fla file is available on my site for download at the below address.

http://blueapplestudio.com/FlashTrouble/HitTestArrayNestedClips.zip

Any further help is still appreciated.
adam NO[at]SPAM blueapplestudio
10/11/2006 2:03:11 PM
I adjusted your (AnandMX ) code a bit and got it to work. However, I need to
have the hitTest performed outside of the for statement that attaches the
MovieClips. There are a number of dynamic things done to the mcContainer clip
before the hitTest needs to be performed.

Attached is the modified code. If anyone knows how to strip the hitTest out
and structure it like the code in my original post, feel free to chime in.

Thanks.

//Defines a string.
var sShapeList:String = "Pentagon,Pentagon,Square,Triangle,Square,Pentagon";

//Creates a string from the string sShapeList.
var aShapeList:Array = sShapeList.split(",");

//Creates an empty MovieClip, and sets its position.
this.createEmptyMovieClip("mcContainer", this.getNextHighestDepth());
mcContainer._y = 230;

//Creates an empty array that will hold the instance names of the MCs found in
mcContainer.
var aInstanceNamesInMCContainer:Array = [];

//Attaches MovieClips from the library into mcContainer.
//Performs a hitTest on each of the 6 MovieClips within the mcContainer clip.
for (i = 0; i < aShapeList.length; i++){
var tmpMov:MovieClip= mcContainer.attachMovie(aShapeList[i], aShapeList[i]+i,
mcContainer.getNextHighestDepth(), {_x: i * 95});
aInstanceNamesInMCContainer.push(aShapeList+i);
trace("Did "+aShapeList[i]+i+" touch mcTarget1?: "+tmpMov.hitTest(mcTarget1));
}

stop();
coldMiner
10/11/2006 2:24:45 PM
Tested following code with your .fla:

//Defines a string.
var sShapeList:String = "Pentagon,Pentagon,Square,Triangle,Square,Pentagon";
//Creates a string from the string sShapeList.
var aShapeList:Array = sShapeList.split(",");
//Creates an empty MovieClip, and sets its position.
this.createEmptyMovieClip("mcContainer", this.getNextHighestDepth());
mcContainer._y = 230;
//Creates an empty array that will hold the instance names of the MCs found in
mcContainer.
var aInstanceNamesInMCContainer:Array = new Array();
//Attaches MovieClips from the library into mcContainer.
for (i=0; i<aShapeList.length; i++) {
var mmc:MovieClip;
mmc = mcContainer.attachMovie(aShapeList[i], aShapeList[i]+i,
mcContainer.getNextHighestDepth(), {_x:i*95});
aInstanceNamesInMCContainer.push(mmc);
}
// a hitTest function
function theHitTest(who) {
trace("*** HIT TEST | "+who+" ***");
trace("----------------------------------");
for (i=1; i<aInstanceNamesInMCContainer.length; i++) {
var ht = who.hitTest(aInstanceNamesInMCContainer[i]);
trace(ht+" -> "+aInstanceNamesInMCContainer[i]);
trace("----------------------------------");
}
trace("\n\n");

}
// usage:
theHitTest(mcTarget1);
theHitTest(mcTarget2);
theHitTest(mcTarget3);
adam NO[at]SPAM blueapplestudio
10/11/2006 6:14:01 PM
coldMiner, you nailed it. Just had to change the initial value of i to 0 in
your hitTest function so it checks the first index as well. I appreciate your
and AnandMX's hard work on this.

Take care.
coldMiner
10/11/2006 6:22:33 PM
[q][i]Originally posted by: [b][b]adam@blueapplestudio[/b][/b][/i]
coldMiner, you nailed it. Just had to change the initial value of i to 0 in
your hitTest function so it checks the first index as well. I appreciate your
and AnandMX's hard work on this.

Take care.[/q]
Good!
the inital value of 0 vs 1 was because of copy/paste of my first post (where I
tested hitTest against the first array[0].
AddThis Social Bookmark Button