AS3 删除动态创建的子影片剪辑
Posted
技术标签:
【中文标题】AS3 删除动态创建的子影片剪辑【英文标题】:AS3 removing dynamically created child movieclips 【发布时间】:2012-05-25 13:30:12 【问题描述】:我对 AS3 还很陌生。无论如何,我尝试在单击时删除动态创建的子影片剪辑。当一个污垢块被点击时,它是“世界”的一个子动画片段,我想将其移除。
我尝试了各种使用 removeChild 删除它的方法。我还尝试在创建影片剪辑的 for 循环内部/外部移动函数。
var blockCount:Number = 0;
var blockArray:Array = [];
var world:MovieClip = new World();
world.x = 50;
world.y = 50;
world.name = "world";
addChild(world);
for(var i:Number=1;i<=100;i++)
blockCount++;
var tempGrassBlock:MovieClip = new GrassBlock();
tempGrassBlock.x = i*16;
tempGrassBlock.y = 256;
tempGrassBlock.name = "b"+blockCount;
world.addChild(tempGrassBlock);
tempGrassBlock.addEventListener(MouseEvent.CLICK, removeBlock);
function removeBlock(event:Event)
world.removeChild(getChildByName(event.target.name));
感谢您的帮助。
【问题讨论】:
【参考方案1】:试试这个
function removeBlock(event:Event)
world.removeChild(event.currentTarget as DisplayObject);
【讨论】:
【参考方案2】:任何函数定义都不应该在 for 中。我在你的代码中改变了它,并在下面重写了一点:
var blockCount:Number = 0;
var blockArray:Array = [];
var world:MovieClip = new World();
world.x = 50;
world.y = 50;
world.name = "world";
addChild(world);
for(var i:Number=1;i<=100;i++)
blockCount++;
var tempGrassBlock:MovieClip = new GrassBlock();
tempGrassBlock.x = i*16;
tempGrassBlock.y = 256;
tempGrassBlock.name = "b"+blockCount;
world.addChild(tempGrassBlock);
tempGrassBlock.addEventListener(MouseEvent.CLICK, removeBlock);
function removeBlock(event:MouseEvent)
trace("Is click really working? This target name is " + event.currentTarget.name);
world.removeChild(event.currentTarget));
【讨论】:
以上是关于AS3 删除动态创建的子影片剪辑的主要内容,如果未能解决你的问题,请参考以下文章