使用数组,我创建了一个由tile组成的MovieClip。我需要动态创建网格,因为有多个大小选项。如果用户选择不同的网格大小,我需要在添加新网格之前删除当前的网格 - 这是我遇到问题的地方。以下是创建网格的代码:
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.addChild(gridItems[i]);
}
grid.x = stage.stageWidth/ 2 - grid.width/2;
grid.y = (stage.stageHeight - 100) / 2 - grid.height/2;
}
如何从其他功能中删除网格?似乎每当我离开createGrid()函数时,对网格的引用都会丢失。
感谢您的任何建议!
以下完整代码。下拉菜单来自http://www.codingcolor.com/as3/as3-drop-down-menu/ - 我知道代码很乱......这是一项正在进行的工作。
package {
import flash.display.MovieClip;
import flash.events.*;
import flash.geom.*;
import flash.display.*;
import flash.ui.*;
import flash.text.*;
import com.dropdown.DropDown;
import fl.controls.*;
public class sp2h extends MovieClip {
public var gridSize:Number = 225;
public var gridBreak:Number = 15;
private var gridItems = new Array();
private var grid:MovieClip = new MovieClip();
public var rotate:Number = 1;
public var gridStatus:Number = 0;
public var dropDwn:MovieClip;
public var t:TextField;
public var dropDArray:Array = new Array({label:"Size 0 - 15x15", data:225},{label:"Size 1 - 18x15", data:270},{label:"Size 2 - 18x18", data:324},
{label:"Size 3 - 21x18", data:378},{label:"Size 4 - 21x21", data:441},{label:"Size 5 - 24x21", data:504},
{label:"Size 6 - 24x24", data:576},{label:"Size 7 - 27x24", data:648},{label:"Size 8 - 27x27", data:729},
{label:"Size 9 - 30x27", data:810},{label:"Size 10 - 30x30", data:900},{label:"Size 11 - 33x30", data:990},
{label:"Size 12 - 33x33", data:1089},{label:"Size 13 - 36x33", data:1188},{label:"Size 14 - 36x36", data:1296},
{label:"Size 15 - 39x36", data:1404},{label:"Size 16 - 39x39", data:1521},{label:"Size 17 - 42x39", data:1638},
{label:"Size 18 - 42x42", data:1764});
public var dropDArray2:Array = new Array({label:"Hip & Stylish", data:0},{label:"Christmas", data:1},{label:"Easter", data:2},{label:"Slick", data:3});
public function sp2h() {
createGrid();
dropDwn = new DropDown();
dropDwn.x = 50;
dropDwn.y = 820;
dropDwn.init(dropDArray,150, 25,"up");
addChild(dropDwn);
dropDwn.addEventListener(Event.CHANGE,onDropDown);
createStatusField();
function createStatusField():void
{
t = new TextField();
t.name = "tField";
t.x = 350;
t.y = 100;
t.selectable = false;
t.autoSize = TextFieldAutoSize.CENTER;
addChild(t);
}
function onDropDown(event:Event)
{
t.text = event.target.selectedObject.label;
gridSize = event.target.selectedObject.data;
switch (gridSize){
case 225:
gridBreak = 15;
break;
case 270:
gridBreak = 18;
break;
case 324:
gridBreak = 18;
break;
case 378:
gridBreak = 21;
break;
case 441:
gridBreak = 21;
break;
case 504:
gridBreak = 24;
break;
case 576:
gridBreak = 24;
break;
case 648:
gridBreak = 27;
break;
case 729:
gridBreak = 27;
break;
case 810:
gridBreak = 30;
break;
case 900:
gridBreak = 30;
break;
case 990:
gridBreak = 33;
break;
case 1089:
gridBreak = 33;
break;
case 1188:
gridBreak = 36;
break;
case 1296:
gridBreak = 36;
break;
case 1404:
gridBreak = 39;
break;
case 1521:
gridBreak = 39;
break;
case 1638:
gridBreak = 42;
break;
case 1764:
gridBreak = 42;
break;
}
createGrid();
}
wtt1.addEventListener(MouseEvent.MOUSE_DOWN, wtt1_ClickToDrag);
function wtt1_ClickToDrag(event:MouseEvent):void
{
wtt1.startDrag();
stage.addEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
function myKeyDown(e:KeyboardEvent):void{
if (e.keyCode == Keyboard.SPACE){
rotate = rotate + 1;
if (rotate == 5){
rotate = 1;
}
trace(rotate);
wtt1.gotoAndStop(rotate);
}
}
stage.addEventListener(MouseEvent.MOUSE_UP, wtt1_ReleaseToDrop);
function wtt1_ReleaseToDrop(event:MouseEvent):void
{
wtt1.stopDrag();
stage.removeEventListener(KeyboardEvent.KEY_DOWN, myKeyDown);
}}}
public function createGrid() {
removeGrid(); //a separate function that removes your grid
addChildAt(grid, 0);
gridStatus = 1;
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.addChild(gridItems[i]);
}
//align to center
grid.x = stage.stageWidth/ 2 - grid.width/2;
grid.y = (stage.stageHeight - 100) / 2 - grid.height/2;
//send to back
addChildAt(bggrid,0);
}
public function removeGrid():void {
if(grid && grid.parent) removeChild(grid);
if (contains(grid)) {
trace("grid is on still on stage")
}
trace(grid.parent)
}}}
答案 0 :(得分:2)
变量仅适用于它们(以及任何子范围)中定义的范围。在您的情况下,范围是您的函数(createGrid
)。因此,该范围之外的任何内容({
}
大括号)都无法访问这些变量。
另外值得注意的是,一旦函数完成执行,这些变量就没有持久性。因此,下次该函数运行时,将创建具有新值的所有新变量。
您需要在函数之外声明稍后要使用的变量。然后,如果您需要删除以前的网格项,可以在创建新网格项之前检查并执行此操作。
这样的事情:
private var gridItems:Array;
private var grid:MovieClip;
public function createGrid() {
removeGrid(); //a separate function that removes your grid
gridItems = new Array();
grid = 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.addChild(gridItems[i]);
}
grid.x = stage.stageWidth/ 2 - grid.width/2;
grid.y = (stage.stageHeight - 100) / 2 - grid.height/2;
}
public function removeGrid():void {
//if the grid has a value already and has a parent (is on the stage), remove it
if(grid && grid.parent) removeChild(grid);
}