vendredi 29 mai 2015

AS3 Flash - How to remove dynamically created MovieClip from different function?

Using an array, I've created a MovieClip made up of tiles. I need to create the grid dynamically as there are multiple size options. If the user selects a different grid size, I need to remove the grid currently in place before adding the new grid - that is where I'm having trouble. Here is the code for creating the grid:

    public function createGrid() {

    var gridItems = new Array();
    var grid:MovieClip = new MovieClip();
    addChildAt(grid, 0);

    for (var i:int = 0; i < gridSize; i++) {
    gridItems[i] = new tile();
    gridItems[i].x = (i % gridBreak) * (gridItems[i].width );
    gridItems[i].y = int(i / gridBreak) * (gridItems[i].height);

    grid.x = stage.stageWidth/ 2 - grid.width/2;
    grid.y = (stage.stageHeight - 100) / 2 - grid.height/2;


How can I remove grid from within a different function? It seems that whenever I leave the createGrid() function the reference to grid is lost.

Thanks for any suggestions!

Aucun commentaire:

Enregistrer un commentaire