Question

Hey guys so Basically what I'm trying to do is when my player collides with all 5 of the points MovieClips in an array and the Goal Movie Clip i want a text to appear saying "Perfect. But i cant quite accomplish this. I set up the function and it seems like it would but i think what might be wrong is the hitTest with all the Movie clips in the array.

Here is how i set it up:

This is in my loop Function:

private function gameLoop(e:Event):void 
    {

        perfectTextFunction();

            }

This is the function:

private function perfectTextFunction():void 
    {


        if (player.hitTestObject(mcGoal_1 && points))
        {
            trace("perfect Text");
                    mcPerfect = new perfectText();
                    mcPerfect.x = (stage.stageWidth / 2);
                    mcPerfect.y = (stage.stageHeight/ 2);
                    stage.addChild(mcPerfect);

        }
    }

The Trace doesn't pick anything up.

Here is how the points are added to the stage if needed:

public function addPointsToStage():void
    {            
         for (var i = 0; i < nPoints; i++)
         {
             trace(aPointsArray.length);
             points = new mcGainPoints();
             stage.addChild(points);
             points.x = startPoint.x + (xSpacing * i);
             points.y = startPoint.y - (ySpacing * i);
             aPointsArray.push(points);
         }

    }

Please any help would be appreciated! THank you!

VESPER here is how I made the NESTED LOOP:

//If all points are hit then Perfect Hit Text
                if (player.hitTestObject(mcGoal_1 || mcGoal_2)) 
                { 
                        var weHitAll:Boolean = true;

                    for (var s in aPointsArray)
                    {
                          weHitAll = weHitAll && player.hitTestObject(aPointsArray[s]);
                           if (!weHitAll) 
                           break;
                    } 
                    if (weHitAll)
                    {

                                trace("perfect Hit");
                                mcPerfect = new perfectText();
                                mcPerfect.x = (stage.stageWidth / 2);
                                mcPerfect.y = (stage.stageHeight/ 2) - 80;
                                stage.addChild(mcPerfect);

                                nScore += 25;
                                updateHighScore();


                    }

                }
Was it helpful?

Solution

var weHitAll:Boolean=true;
for (var s in aPointsArray) {
    weHitAll = weHitAll&&player.hitTestObject(aPointsArray[s]);
    if (!weHitAll) break; // missed one, drop cycle
}
if (weHitAll) {
    trace('Perfect hit!');
    ... // etc
}

Function hitTestObject accepts only a single object as parameter, in order to check against an array you need to iterate through it, checking against one object at a time (here it's in aPointsArray[s]).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top