0
votes

Hey guys so i am developing a Game and i have an Array that keeps track of my 5 movie clips that are added to the stage by the array. I have a Player movie clip as well so the Player movie clip is Hittesting with all the 5 diamond movie clips which are added to the stage like so:

private var nPoints:Number = 5;..........

    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);
         }

    }

Now when the Player comes in contact with all the 5 movie clips everything works fine and the output for the array looks like this:
0 1 2 3 4

Then it continues to the next level.

But say that my Player doesnt hit any of the 5 movie clips or only hits a couple of them, when the next level is started the output looks like this: 5 6 7 8 9

and when the next level is started weird things start happening like points start adding to the highscore by themselves.

I think the problem is that im not destroying the array holding the 5 movie clips correctly this is the code i use to start the next level and destroy the array:

if(player.hitTestObject(mcGoal_1))
           {

            //Remove all points from array/stage
            for (var i:int = 0; i < aPointsArray.length; i++)
            {

                if (aPointsArray[i].parent)
                {
                    parent.removeChild(aPointsArray[i]);
                }
                              startNextLevel();

            }

The mcGoal_1 is the object of the game so if the player hits the goal_1 then destroy all array objects on screen and start next level. This is the startnextlevel function.

private function startNextLevel():void
    {

        //add points to next stage
        addPointsToStage();


    }

So can you see why when the next level starts the array isnt reset back to 01234? I think thats why the game has that bug of randomly adding points. Please any help will be appreciated

3

3 Answers

0
votes
        //Remove all points from array/stage
        for (var i:int = 0; i < aPointsArray.length; i++)
            if (aPointsArray[i].parent)
                parent.removeChild(aPointsArray[i]);
        // just one statement, no braces
        aPointsArray.length=0; // clear the array itself, not just 
        // points from display list!
        startNextLevel(); // now start level

An incorrect order of correct statements leads to a disaster. Always check your code flow, what executes now and what then, and how many times.

0
votes

It doesn't look like you're removing anything from the array.

You can use a combination of .indexOf() and .splice() to remove items from an array:

function removeMc(clip:MovieClip):void
{
    if(clip.parent) clip.parent.removeChild(clip);

    // Also remove from array.
    var index:int = aPointsArray.indexOf(clip);
    aPointsArray.splice(index, 1);
}

It might also be worth simply emptying the array when you startNextLevel():

private function startNextLevel():void
{
    aPointsArray.length = 0;

    //add points to next stage
    addPointsToStage();
}
0
votes

Just assign your array to null value. so that array will start from begining whenever you require. i hope the following code code would helps you.

  var nPoints:Number = 5;
  var points:poin;
  var aPointsArray:Array;
   var startPoint:Number = 0;
  var xSpacing:Number = 20;  
    var ySpacing:Number = 20; 

    addPointsToStage();

  function addPointsToStage():void
    {        
    aPointsArray = null;
        aPointsArray = new Array();

         for (var i:int = 0; i < 6; i++)
         {
             trace(aPointsArray.length);
             points = new poin();
             addChild(points);
              points.x = 100+ (xSpacing * i);
             points.y = 200 - (ySpacing * i);
             aPointsArray.push(points);
         }
    }