Skip to main content
kas8
Known Participant
March 29, 2011
Answered

Hittest only working with 1 added child

  • March 29, 2011
  • 1 reply
  • 815 views

Ive got this piece of script where i place objects on the stage, to hittest eachother.

Ive got two seperate addchilds: one child must hittest another child

But for some reason it will only hittest with one object from the 2nd addchild 😕😕

this is the addchild for the hittesting objects:

all variables are defined properly

for(var ringteller=0; ringteller<2; ringteller++) {
for(var teller1=1; teller1<17; teller1++) {
    //var ringsegment_mc:Ring_1=new Ring_1();
    var ClassReference:Class = getDefinitionByName("Ring_"+teller1) as Class;
    var ringsegment_mc:MovieClip=new ClassReference();
    ringsegment_mc.addEventListener(Event.ENTER_FRAME,vernietig);
    addChild(ringsegment_mc);
    }
}

for(var teller2=0; teller2<2; teller2++) {
    var hitte_mc:Hitte=new Hitte();
    Hittes.push(hitte_mc);
    hitte_mc.mousedown=0;
    hitte_mc.addEventListener(Event.ENTER_FRAME, floaten);
    hitte_mc.addEventListener(MouseEvent.MOUSE_DOWN, slepenStart);
    hitte_mc.addEventListener(MouseEvent.MOUSE_UP, slepenStop);
    addChild(hitte_mc);
}

and this is the code for the slepenStart and vernietig:

function vernietig(evt:Event) {
    for(var teller3:int=1;teller3<Hittes.length;teller3++){
        if (evt.currentTarget.hitTestObject(Hittes[teller3]) ) {
               evt.currentTarget.alpha+= -0.1;
            if (evt.currentTarget.alpha<=0.05) {
                evt.currentTarget.x=evt.currentTarget.y=-200;
                this["segmentenkapot"+evt.currentTarget.kerngroep]+=1;
            }
        }
        if (this["segmentenkapot"+evt.currentTarget.kerngroep] == 16) {
            trace("ring kapot");
        }
    }
}

function slepenStart (evt:MouseEvent):void {
    evt.currentTarget.startDrag();
    evt.currentTarget.snelheidx=evt.currentTarget.snelheidy=0;
    evt.currentTarget.mousedown=1;
}
function slepenStop (evt:MouseEvent):void {
    evt.currentTarget.stopDrag();
    evt.currentTarget.snelheidx=4; evt.currentTarget.snelheidy=2;
    evt.currentTarget.mousedown=0;
}

so: only one instance of the hitte_mc will hittest with the instances of Ring_

what did i miss to fix this 😕😕

This topic has been closed for replies.
Correct answer kglad

you're checking for a hittest with two of your hitte_mc's.  fix your for-loop to check for all elements of Hittes.

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
March 29, 2011

you're checking for a hittest with two of your hitte_mc's.  fix your for-loop to check for all elements of Hittes.

kas8
kas8Author
Known Participant
March 29, 2011

i replaced

teller3:int=1;

with

teller3:int=0;

which seems to have done it ^^

that is what you meant, right?

btw: the for loop was only checking for one, instead of the whole array ^^

kglad
Community Expert
Community Expert
March 29, 2011

yes, that's what i meant.