2010-02-18 67 views
0

我想制作从外部XML加载数据并显示相关含义的自定义字典组件。 这里是XML文件制作自定义词汇表

<glossary> 
<alphabet id="A"> 
    <term heading= "Anchor" definition="A mechanical device that prevents a vessel from moving"/> 
    <term heading= "Atlas" definition="A collection of maps in book form"/> 
</alphabet> 
<alphabet id="D"> 
    <term heading= "Delay" definition="Time during which some action is awaited"/> 
</alphabet> 
<alphabet id="D"> 
    <term heading= "Risk" definition="A source of danger; a possibility of incurring loss or misfortune"/> 
    <term heading= "Rotate" definition="Turn on or around an axis or a center"/> 
</alphabet> 
</glossary> 

下面是脚本。它建议立即进行删除显示相关的定义,当我们点击期限:

var xmlLoader:URLLoader= new URLLoader() 
xmlLoader.load(new URLRequest("datalist.xml")) 
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded) 
var xmlData:XML= new XML() 
//This function is called when the XML file is loaded 
function xmlLoaded(e:Event):void { 

//Make sure we're not working with a null loader 
if ((e.target as URLLoader) != null) { 
    //Insert the loaded data to our XML variable 
    xmlData= new XML(e.target.data) 
    xmlData.ignoreWhitespace = true; 
    //Call the function that creates the whole menu 
    createMenu(); 
} 
} 
function createMenu():void { 
//This will be used to represent a menu item 
var menuItem:MenuItem 
//Counter 
var i:uint = 0; 
//Loop through the links found in the XML file 
for each (var link:XML in xmlData.alphabet.term) { 
    menuItem = new MenuItem(); 
    //Insert the menu text ([email protected] reads the link's "name" attribute) 
    menuItem.menuLabel.text = [email protected]; 
    //If the text is longer than the textfield, autosize so that the text is 
    //treated as left-justified text 
    menuItem.menuLabel.autoSize = TextFieldAutoSize.LEFT; 
    //Insert the menu button to stage 
    menuItem.x = 20; 
    menuItem.y = 30 + i*25.3; 
    //Make the button look like a button (hand cursor) 
    menuItem.buttonMode = true; 
    menuItem.mouseChildren = false; 
    //Add event handlers (used for animating the buttons) 
    menuItem.addEventListener(MouseEvent.MOUSE_DOWN, mouseDownHandler); 
    //menuItem.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler); 
    addChild(menuItem); 
    //Increment the menu button counter, so we know how many buttons there are 
    i++; 
} 
} 

function mouseDownHandler(e:Event):void 
{ 
var trace([email protected]) 
} 

回答

0

你有不同的方式来实现你想要什么:

1您可以添加到您的类菜单项现场将举行定义你要

public class MenuItem { 
//... 
public var definition:String; 
//... 
} 
//... 
function createMenu():void { 
//This will be used to represent a menu item 
var menuItem:MenuItem 
//Counter 
var i:uint = 0; 
//Loop through the links found in the XML file 
for each (var link:XML in xmlData.alphabet.term) { 
    menuItem = new MenuItem(); 
    //Insert the menu text ([email protected] reads the link's "name" attribute) 
    menuItem.menuLabel.text = [email protected]; 
    menuItem.definition = [email protected]; 
//... 
} 
} 
//... 
function mouseDownHandler(e:Event):void 
{ 
var mi:MenuItem=e.currentTarget as MenuItem; 
if (mi!==null) 
    trace(mi.definition); 
} 

2 - 使用dictionary将标题映射到定义:

//... 
import flash.utils.Dictionary; 
//... 
var maps:Dictionary=new Dictionary(true); 
//... 
function createMenu():void { 
//This will be used to represent a menu item 
var menuItem:MenuItem 
//Counter 
var i:uint = 0; 
//Loop through the links found in the XML file 
for each (var link:XML in xmlData.alphabet.term) { 
    menuItem = new MenuItem(); 
    //Insert the menu text ([email protected] reads the link's "name" attribute) 
    menuItem.menuLabel.text = [email protected]; 
    maps[menuItem][email protected](); 
//... 
} 
} 
//... 
function mouseDownHandler(e:Event):void 
{ 
var mi:MenuItem=e.currentTarget as MenuItem; 
if (mi!==null) 
    trace(maps[mi]); 
} 

3 - 使用e4x搜索,找到您的文本标签权的定义

//... 
function mouseDownHandler(e:Event):void 
{ 
var mi:MenuItem=e.currentTarget as MenuItem; 
if (mi!==null) { 
    var heading:String=mi.menuLabel.text; 
    trace(xmlData.alphabet.term.(@heading.toString()==heading)[email protected]); 
} 
}