Nathan
Nathan

Reputation: 536

HitTest Multiple MovieClips with Array

I have a MovieClip exported to ActionScript called smallGainPoints. What i want to do is create multiple instances of this MovieClip on the stage in a linear or diagonal path. When this is accomplished i want a hitTestObject to take place between the Points array and the Player. The points are added to stage where i would want them to be, but the HitTest wont initiate.

Here is how i set up the Functions:

This Function is added in my Gameloop which is called from an onEnterFrame handler:

private function checkPlayerHitPoints():void 
{
    for (var j:int = 0; j < aPointsArray.length; j++)
    {
        //get current points in j loop
        var currentPoints:smallGainPoints = aPointsArray[j];

        //test if player is hitting current point
        if (player.hitTestObject(currentPoints))
        {
            //remove point on stage
            currentPoints.destroyPoints()
            //remove point from array
            aPointsArray.splice(j, 1);

            nScore += 5;
            updateHighScore();
        }
    }
}

Im not sure if i did this right but i want to add multiple instances of the points in a line so the player can gather as much points as possible. So i created a function and set the positions then added the function in my constructor as so addPointsToStage() so it doesn't loop every frame.

private function addPointsToStage():void
{
    for (var i = 0; i < nPoints; i++)
    {
        points = new smallGainPoints();
        stage.addChild(points);
        points.x = (stage.stageWidth / 2);
        points.y = (stage.stageHeight / 2);

        points = new smallGainPoints();
        stage.addChild(points);
        points.x = (stage.stageWidth / 2) + 200;
        points.y = (stage.stageHeight / 2);
    }

This is how I initiated the array:

public var points:smallGainPoints;
    private var nPoints:Number = 5;
    private var aPointsArray:Array;

Then in my Constructor i added:

aPointsArray = new Array();

So the points are added to the stage but the hittest doesnt work. Please help!

Upvotes: 0

Views: 1120

Answers (1)

Adam Harte
Adam Harte

Reputation: 10500

In your addPointsToStage method, you never add your smallGainPoints object to the array.

After this line:

points = new smallGainPoints();

Push the new points object onto the aPointsArray array:

aPointsArray.push(points);

EDIT:

A better way to add your points in a row might be like this:

private function addPointsToStage():void
{
    var startPoint:Point = new Point(stage.stageWidth / 2, stage.stageHeight / 2);
    var spacing:Number = 50;

    for (var i = 0; i < nPoints; i++)
    {
        points = new smallGainPoints();
        aPointsArray.push(points);
        addChild(points);
        points.x = startPoint.x + (spacing * i);
        points.y = startPoint.y;
    }
}

This for loop will add a bunch of smallGainPoint objects in a row, starting from the center of the screen and going right.

Upvotes: 1

Related Questions