Search code examples
functionactionscript-3hide

AS3 Error 1119 undefined property with static type Function


I am new to as3 and programming in general and I want to ask how to hide 'function grid()' entirely when 'mc_gameover' event is clicked?

I have tried adjusting its visibility and removing the children of the 'tile' variable, using 'stage.parent.removeChild(tile);', but it only removes one tile.

function grid(): void

{

mc_gameover.btn_mainmenu.addEventListener(MouseEvent.CLICK, hidetile);

function hidetile(event:MouseEvent):void
{   

grid.visible= false;
removeChild(tile);

}   
    
    
for(var i:int = 0; i < rows; i++){

for(var j:int = 0; j < columns; j++){

var tile:Sprite = new Tile(); //get tile from the library

//Arrange tiles to form a grid

tile.x = (headWidth/2) + (j * headWidth);

tile.y = (headWidth/2) + (i * headWidth);

addChild(tile);
    

}

}


}

grid(); 

Solution

  • Why do you think it is not possible? It is possible in several ways. The most straightforward and comprehensible one is to devise a container for all the things you want to show/hide at once.

    // Might not be a mandatory section here,
    // but will be when you get to work with classes.
    import Tile;
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    
    // Container for all the tiles.
    var WholeGrid:Sprite;
    
    // Do not nest functions.
    // Although it is possible, it is not a good thing to do.
    function hideGrid(event:MouseEvent):void
    {
        // Use tabs to keep your code structured and readable.
        if (WholeGrid)
        {
            WholeGrid.visible = false;
        }
    }
    
    function arrangeGrid():void
    {
        // Create the container, if necessary. Clean and show it otherwise.
        if (WholeGrid)
        {
            WholeGrid.removeChildren();
            WholeGrid.visible = true;
        }
        else
        {
            WholeGrid = new Sprite;
            addChild(WholeGrid);
        }
        
        mc_gameover.btn_mainmenu.addEventListener(MouseEvent.CLICK, hideGrid);
      
        for (var i:int = 0; i < rows; i++)
        {
            for (var j:int = 0; j < columns; j++)
            {
                // Instantiate a new tile from library class.
                var tile:Sprite = new Tile;
                
                // Arrange the tile to form a grid.
                // No need for brackets here, as * and / has prioroty.
                tile.x = headWidth / 2 + j * headWidth;
                tile.y = headWidth / 2 + i * headWidth;
                
                // Add the tile to the container.
                WholeGrid.addChild(tile);
            }
        }
    }
    
    // Create and arrange the grid.
    arrangeGrid();