很简单。销毁现有实例并创建一个新实例。此外,不要在其他函数中声明函数。
public class Lagegridd extends MovieClip
{
public function Lagegridd()
{
btn_1.addEventListener(MouseEvent.CLICK, onClick);
}
private var currentGrid:Tabellkamp;
private var gridSource:Array = ["file1.xml", "file2.xml", "file3.xml"];
private function onClick(e:MouseEvent):void
{
// Obtain the first element from the list.
var anUrl:String = gridSource.shift();
changeGrid(anUrl);
}
private function changeGrid(url:String):void
{
if (currentGrid)
{
removeChild(currentGrid);
// Another cleanup routines here, if necessary.
}
currentGrid = new Tabellkamp;
// You need to define this function instead of loading
// data from "link1" inside the object constructor.
currentGrid.loadData(url);
addChild(currentGrid);
}
}
:OOP示例。
package
{
import flash.display.Sprite;
import flash.system.System;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLRequest;
import flash.net.URLLoader;
// Don't use MovieClip if you don't have frames and timelines.
public class LasteinnXML extends Sprite
{
public var url:String;
public var loader:URLLoader;
public var dataProvider:XML;
public function load(path:String):void
{
url = path;
loader = new URLLoader;
loader.addEventListener(Event.COMPLETE, onData);
// Always handle erroneous cases. Last three arguments are there
// because it is wise to not unsubscribe from these events but
// let the garbage collector decide when to destroy them.
loader.addEventListener(IOErrorEvent.IO_ERROR, onError, false, 0, true);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onError, false, 0, true);
// You don't need to store the request object.
loader.load(new URLRequest(url));
}
private function onData(e:Event):void
{
try
{
dataProvider = new XML(loader.data);
finishLoading();
}
catch (fail:Error)
{
// There's another erroneous case even if loading is fine:
// Invalid XML data. Always chack for it as well.
onError(null);
}
// Handle the SUCCESSFUL case here.
}
private function finishLoading():void
{
// Cleanup routines. Dispose of your objects once you don't need them.
if (!loader) return;
loader.removeEventListener(Event.COMPLETE, onData);
loader = null;
}
private function onError(e:Event):void
{
// In case the object was already destroyed
if (!loader) return;
finishLoading();
// Handle the ERRONEOUS case here.
}
public function destroy():void
{
finishLoading();
// The recommended way of cleaning XML data up.
if (dataProvider)
{
System.disposeXML(dataProvider);
dataProvider = null;
}
}
}
}
用法:
var A:LasteinnXML = new LasteinnXML;
var B:LasteinnXML = new LasteinnXML;
var C:LasteinnXML = new LasteinnXML;
addChild(A);
addChild(B);
addChild(C);
A.load("filea.xml");
B.load("fileb.xml");
C.load("filec.xml");
C.destroy();
removeChild(C);