PDA

View Full Version : for each



brndn
October 9th, 2008, 08:17 AM
for each(var b:MovieClip in SYM)
{
for each(var c:MovieClip in SYM_MAIN)
{
if(b.hitTestObject(c)) trace("****in winner")
}
}

could anyone help me with this. I have these loops but they dont do exactly what I am wanting.

i want the items in the arrays to match up.
this way they all trace.

if you cast your eye at these two arrays you should get my probelm

var SYM:Array = [cartouche.symbol1, cartouche.symbol2, cartouche.symbol3, cartouche.symbol4];

var SYM_MAIN:Array = [emblem.symbol1, emblem.symbol2, emblem.symbol3, emblem.symbol4];


cheers if anyone can help out.

brndn

BeerOclock
October 9th, 2008, 03:45 PM
Are you sure that your symbols have actual area? If they are simply created with new MovieClip() or whatever, then I dont know if hitTestObject will ever return true. I believe you need the objects to actually contain some visible area. Look at the example from live docs:

var circle1:Shape = new Shape();
circle1.graphics.beginFill(0x0000FF);
circle1.graphics.drawCircle(40, 40, 40);
addChild(circle1);

I think the hitTest would fail if it wasnt for the graphics being drawn on the sprite.
Also, make sure the symbols are added to the stage as well, via addChild() somewhere.
Finally, your trace statement doesnt end in a semicoloon; , donno how you got away with that!

Hope this helps, im new at this !

brndn
October 9th, 2008, 09:15 PM
hey
thanks for the reply.
The hit test does work on these objects. it however applies an if statment for all rather than the corresponding object in the corresponding array.

cartouche.symbol1

has a hit of true for emblem.symbol1

at the moment the two loops create this

if(cartouche.symbol1.hitTestObject(emblem.symbol1) )
if(cartouche.symbol1.hitTestObject(emblem.symbol2) )
if(cartouche.symbol1.hitTestObject(emblem.symbol3) )

so all items return true.

but yes this hit metod does work, rather my loop is not targeting the correct item instead it targets all items.

if that makes sense


thanks for your reply

brndn

BeerOclock
October 10th, 2008, 02:11 AM
Hmm, sorry I guess I dont understand what you mean then...

If you want to test cartouche1 against emblem1 and cartouche2 against emblem2 etc... then I think you simply need something like this:

for (var i:int=0; i<SYM.length; i++) {
if (MovieClip(SYM[i]).hitTestObject(MovieClip(SYM_MAIN[i]))) {
trace ("******in winner lol");
}
}

I have not syntax checked that..

theCodeBot
October 10th, 2008, 06:56 AM
Hmm, sorry I guess I dont understand what you mean then...

If you want to test cartouche1 against emblem1 and cartouche2 against emblem2 etc... then I think you simply need something like this:

for (var i:int=0; i<SYM.length; i++) {
if (MovieClip(SYM[i]).hitTestObject(MovieClip(SYM_MAIN[i]))) {
trace ("******in winner lol");
}
}

I have not syntax checked that..
That's right.
That will test 1 to 1, 2 to 2, and ignore the rest of the matchups.
Your original says "go through each cartouche. For each cartouche, go through all the emblems." which does 1 to 1m 1 to 2, 1 to 3, 2 to 1....

brndn
October 10th, 2008, 08:04 AM
thats perfect dude. have not tested but your explaination is spot on.
unless the movieClip is not allowed. ill check it. cheers

brndn
October 11th, 2008, 03:39 AM
import fl.transitions.Tween;
import fl.transitions.easing.*;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
stop();
var SYM:Array = [cartouche.symbol1, cartouche.symbol2, cartouche.symbol3, cartouche.symbol4, cartouche.symbol5, cartouche.symbol6];
var SYM_MAIN:Array = [emblem.symbol1, emblem.symbol2, emblem.symbol3, emblem.symbol4, emblem.symbol5, emblem.symbol6];

var startX:Number;
var startY:Number;
var counter:Number = 0;
var cartouche_InOut:Boolean;
var dragStatus:Boolean;

for ( var i in SYM ) {
var cartouche_var:MovieClip = MovieClip(SYM[i])
var emblem_var:MovieClip = MovieClip(SYM_MAIN[i])
cartouche_var.addEventListener(MouseEvent.MOUSE_DO WN, pickUp);
cartouche_var.addEventListener(MouseEvent.MOUSE_UP , dropIt);
cartouche_var.buttonMode = true;
emblem_var.buttonMode = true;
emblem_var.id = i;
cartouche_var.PARENT = emblem_var;
emblem_var.mouseChildren = false;
cartouche_var.mouseChildren = false;
emblem_var.addEventListener(MouseEvent.ROLL_OVER, tooltipOver);
emblem_var.addEventListener(MouseEvent.ROLL_OUT, tooltipOff);
}




function pickUp(event:MouseEvent):void {
event.target.startDrag(true);
event.target.parent.addChild(event.target);
startX = event.target.x;
startY = event.target.y;
}


function dropIt(event:MouseEvent):void {
event.target.stopDrag();
var TARGET:DisplayObject = event.target;
var PARENT:DisplayObject = event.target.PARENT;

if (TARGET.hitTestObject(PARENT)){
trace("Good Job!");

event.target.removeEventListener(MouseEvent.MOUSE_ DOWN, pickUp);
event.target.removeEventListener(MouseEvent.MOUSE_ UP, dropIt);
event.target.buttonMode = false;
cartouche.removeChild(event.target);
counter++;
} else {
trace("Try Again!");
event.target.x = startX;
event.target.y = startY;
}


if(counter == SYM.length){
trace("Congrats, you're finished!");

}
}

I Knew I had it in me.
heres a working tested hittester.

so i just assigned an PARENT object to the eventListener in the main loop.

thank all for your help