0

请帮我。

我正在使用离线地图,并从 XML 读取数据。我将数据放入 DataGrid 中,当我从数据网格中选择项目时,我在舞台中添加子项并为数据创建一个影片剪辑(一次可能有多个子项),但是当我从数据网格中选择另一个项时,新的子项添加了但是旧的没有删除。

这是我的代码:

    familyGrid.addEventListener(Event.CHANGE, get_data2);

function get_data2(event:Event):void {

//get the selected item's name and search in another xml file 

//for the points that have the same name

nameFamilyText.text= familyGrid.selectedItem.Latin;

var pattern_pID2:RegExp;

current_pID2 = [];

pattern_pID2 = new RegExp(nameFamilyText.text);

for (var n:int = 0; n<all_pID2.length; n++)

{

if ((pattern_pID2.test(all_pID2[n].fLatinName) || nameFamilyText.text=="") )

{

current_pID2.push(all_pID2[n]);

var fxnList:XMLList = pID_data2.marker.(@LatinName == nameFamilyText.text);

for each (var xnNode:XML in fxnList)

fButtonName.text= xnNode.@LatinName.toXMLString()

//create marker in the satge which have a n image of offline map

// i need to remove the old one before add a new one

var newString:String = ("id_"+all_pID2[n].id);

var markLoader:Loader = new Loader();

var myMark:String= ("T copy.png");

markLoader.load(new URLRequest (myMark));

markLoader.x=this[newString].x-8;

markLoader.y=this[newString].y-20;

addChild(markLoader);

markLoader.addEventListener(MouseEvent.CLICK, getToolTip);

function getToolTip(e:MouseEvent) {

var flabel:TextField = new TextField();

flabel.width=150;

flabel.height=20;

flabel.border=true;

flabel.background=true;

flabel.autoSize=TextFieldAutoSize.RIGHT;

flabel.text=xnNode.@Species_name.toXMLString();

flabel.x=markLoader.x-30;

flabel.y=markLoader.y-20;

addChild(flabel);

flabel.addEventListener(MouseEvent.CLICK, fShowData);

function fShowData(e:MouseEvent) {

///create movieclip for data    

var fmc:MovieClip = new MovieClip();

fmc.graphics.beginFill(0x000000, .75);

fmc.graphics.drawRect(60, 90, 700, 500);

fmc.graphics.endFill();

fmc.x = 80;

fmc.y = 60;

addChild(fmc);

}}}

updateList2();

}}  
4

1 回答 1

0

只需将要添加的影片剪辑放入容器影片剪辑中即可。也许是这样的:

familyGrid.addEventListener(Event.CHANGE, get_data2);
var familyContainer = new MovieClip();
addChild(familyContainer);

然后在你的 get_data2 函数中

function get_data2(event:Event):void {
    ...
    removePreviousChildren();
    familyContainer.addChild(markLoader);
    ...
}

function removePreviousChildren() {
    while(familyContainer.numChildren > 0) {
        familyContainer.removeChild(familyContainer.getChildAt(0))
    }
}
于 2013-08-05T11:52:49.440 回答