我是 as3 和一般编程的新手,我想问一下如何在单击“mc_gameover”事件时完全隐藏“function grid()”?
我尝试使用“stage.parent.removeChild(tile);”调整它的可见性并删除“tile”变量的子项,但它只删除了一个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();
为什么你认为不可能?这有几种可能。最直接易懂的方法是设计一个容器来容纳所有你想同时显示/隐藏的东西。
// 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.
tile.x = (headWidth / 2) + (j * headWidth);
tile.y = (headWidth / 2) + (i * headWidth);
WholeGrid.addChild(tile);
}
}
}
// Create and arrange the grid.
arrangeGrid();