actionscript-3functionflash-cs5reloadmovieclip

How to remove/reload movieclip/graphic in Flash CS5 with AS3


I have a setupBoard(); and a setupBlocks(); in my function:

function init(e)
{
    setupBoard();
    removeEventListener(Event.ENTER_FRAME , init);
    setupCat();
    setupBlocks();
}

function setupBoard()
{
    
    var columns:Array = new Array();
    var i,j:int;
    var _place:place;

    for (i = 0; i < 11; i++)
    {
        columns = [];
        for (j = 0; j < 11; j++)
        {
            _place = new place();
            _place.thisX=i;
            _place.thisY=j;
            _place.thisDistance=Math.min(i+1,j+1,11-i,11-j)*11;
            _place.y = 56 * i + 3;
            _place.x = 5 + 71 * j + 35*(i%2);
            _place.buttonMode=true;
            _place.addEventListener(MouseEvent.CLICK, setBlock);
            columns[j] = _place;
            // SÆTTER TAL PÅ BRIKKERNE
            _place.thisText.text = _place.thisDistance + " - " + _place.thisX + " : " + _place.thisY;
            addChild(_place);
        }
        rows[i] = columns;
    }
}

The "place" is the MovieClip

this function loads when the game launches and when the game is finish/completed..

the setupBoard, setup the board ofc, and the setupBlocks setup some movieclips, which contain some graphic.

Here's my question, how do I remove/reload all the blocks when the game enters that function again? At the moment they are just placed upon each other, which I don't like at all.


Solution

  • If I understood correctly, what you want to do is remove all the previous blocks (from the last time you ran the setup function) when you run setup a second time.

    To do that, you should create a function which loops your rows and columns Arrays, and for each Place object it find, it does the following: removes it from the stage, removes all Event Listeners, and finally sets it to null. Your function could look something like this (and you would call it just before calling setup again):

    for (i = 0; i < rows.length; i++)
    {
       var column:Array = rows[i];
    
       for (j = 0; j < column.length; j++)
       {
          var place:Place = column[j];
          if (contains(place))
          {
             removeChild(place);
          }
          place.removeEventListener(MouseEvent.CLICK, setBlock);
          place = null;
       }
       column = [];
    }
    row = [];
    

    I just wrote that straight into the box, so it's not tested. But basically it does the three things necessary to get those objects removed from the view, and clears up anything that would stop them from being freed from memory by the garbage collector.

    Hope that helps.

    Debu