简体   繁体   English

如何使用 AS3 在 Flash CS5 中删除/重新加载影片剪辑/图形

[英]How to remove/reload movieclip/graphic in Flash CS5 with AS3

I have a setupBoard();我有一个setupBoard(); and a setupBlocks();和一个setupBlocks(); in my function:在我的功能中:

function init(e)
{
    setupBoard();
    removeEventListener(Event.ENTER_FRAME , init);
    setupCat();
    setupBlocks();
}

function setupBoard()
{
    
    var columns:Array = new Array();
    var i,j:int;
    var _place:place;

    for (i = 0; i < 11; i++)
    {
        columns = [];
        for (j = 0; j < 11; j++)
        {
            _place = new place();
            _place.thisX=i;
            _place.thisY=j;
            _place.thisDistance=Math.min(i+1,j+1,11-i,11-j)*11;
            _place.y = 56 * i + 3;
            _place.x = 5 + 71 * j + 35*(i%2);
            _place.buttonMode=true;
            _place.addEventListener(MouseEvent.CLICK, setBlock);
            columns[j] = _place;
            // SÆTTER TAL PÅ BRIKKERNE
            _place.thisText.text = _place.thisDistance + " - " + _place.thisX + " : " + _place.thisY;
            addChild(_place);
        }
        rows[i] = columns;
    }
}

The "place" is the MovieClip “地方”是电影剪辑

this function loads when the game launches and when the game is finish/completed..此功能会在游戏启动和游戏完成/完成时加载..

the setupBoard, setup the board ofc, and the setupBlocks setup some movieclips, which contain some graphic. setupBoard、set up board ofc 和 setupBlocks 设置一些影片剪辑,其中包含一些图形。

Here's my question, how do I remove/reload all the blocks when the game enters that function again?这是我的问题,当游戏再次进入该功能时,如何删除/重新加载所有块? At the moment they are just placed upon each other, which I don't like at all.目前,它们只是相互叠加,我一点也不喜欢。

If I understood correctly, what you want to do is remove all the previous blocks (from the last time you ran the setup function) when you run setup a second time.如果我理解正确,您想要做的是在第二次运行 setup 时删除所有先前的块(从上次运行 setup 函数开始)。

To do that, you should create a function which loops your rows and columns Arrays, and for each Place object it find, it does the following: removes it from the stage, removes all Event Listeners, and finally sets it to null.为此,您应该创建一个函数来循环您的行和列数组,并且对于它找到的每个 Place 对象,它会执行以下操作:将其从舞台上移除,移除所有事件侦听器,最后将其设置为 null。 Your function could look something like this (and you would call it just before calling setup again):你的函数可能看起来像这样(你会在再次调用 setup 之前调用它):

for (i = 0; i < rows.length; i++)
{
   var column:Array = rows[i];

   for (j = 0; j < column.length; j++)
   {
      var place:Place = column[j];
      if (contains(place))
      {
         removeChild(place);
      }
      place.removeEventListener(MouseEvent.CLICK, setBlock);
      place = null;
   }
   column = [];
}
row = [];

I just wrote that straight into the box, so it's not tested.我只是直接把它写进了盒子里,所以它没有经过测试。 But basically it does the three things necessary to get those objects removed from the view, and clears up anything that would stop them from being freed from memory by the garbage collector.但基本上它完成了从视图中移除这些对象所必需的三件事,并清除了任何会阻止它们被垃圾收集器从内存中释放的东西。

Hope that helps.希望有帮助。

Debu得不

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM