0

所以我有一个菜单,其中包含从 XML 文件动态加载的选项,并且效果很好。他们都为 MouseOvers 附加了 eventListeners,这也很好。我遇到问题的地方是我需要为每个将为该选项加载适当内容的 Click eventListener 添加。每个选项都应根据为该选项指定的地址加载具有可变内容的同一页面。我已经在 XML 文件中列出了每个选项的地址,并将它们推送到一个数组中。我只是不知道如何获得引用它们的选项。到目前为止,这是我的代码:

var blogData:XMLList;
var blogTotal:Number;

var entryTitle:String;

//Arrays
var entriesArray:Array = new Array();
var entryAddr:Array = new Array();

var blogListLoader: URLLoader = new URLLoader();
blogListLoader.load(new URLRequest("BlogEntries.xml"));
blogListLoader.addEventListener(Event.COMPLETE, loadBlogList);

function loadBlogList(e:Event):void
{
    var blogList:XML = new XML(e.target.data);
    blogData = blogList.ENTRY;
    blogTotal = blogData.length();
}


function loadBlogEntries():void
{
    //Load entriesArray
    var yTmp:Number = 0;
    for (var i:Number = 0; i < blogTotal; i++)
    {
        var entryName = blogData[i].@TEXT;
        var entry:TextField = new TextField();
        entriesArray[i] = entry;
        entriesArray[i].textColor = 0x0DAC54;
        entriesArray[i].x = 0;
        entriesArray[i].y = yTmp;
        entriesArray[i].width = 1031;
        entriesArray[i].height = 90.30;
        entriesArray[i].embedFonts = true;
        entriesArray[i].antiAliasType = AntiAliasType.ADVANCED;
        entriesArray[i].defaultTextFormat = listformat;
        entriesArray[i].selectable = false;
        entriesArray[i].wordWrap = true;
        entriesArray[i].text = entryName;
        entriesArray[i].autoSize = TextFieldAutoSize.LEFT;
        entriesArray[i].addEventListener(MouseEvent.MOUSE_OVER , bEntryOvr);
        entriesArray[i].addEventListener(MouseEvent.CLICK , bEntryClick);
        BlogPage.blogEntriesPage.entryList.addChild(entriesArray[i]);
        yTmp = yTmp + 149;

        var Addr = blogData[i].@ADDR;
        entryAddr[i] = Addr;
    }
}

function bEntryOvr(event:MouseEvent):void
{
    event.target.backgroundColor = 0x0DAC54;
    event.target.background = true;
    event.target.textColor = 0x000000;
    event.target.addEventListener(MouseEvent.MOUSE_OUT , bEntryOut);
}

function bEntryOut(event:MouseEvent):void
{
    event.target.background = false;
    event.target.textColor = 0x0DAC54;
    event.target.removeEventListener(MouseEvent.MOUSE_OUT , bEntryOut);
}

function bEntryClick(event:MouseEvent):void
{
    showPage = BlogPage.blogEntriesPage.blogEntry;
    flashup();
}

我不认为这可能有那么难,但它现在让我大吃一惊。任何见解都值得赞赏。谢谢,-T。

4

1 回答 1

1

您可以使用闭包:

entriesArray[i].addEventListener(MouseEvent.CLICK, makeEntryClick(index));
function makeEntryClick(index:Number):Function {
    return function (event:MouseEvent):void {
        bEntryClick(event, index);
    };
}

function bEntryClick(event:MouseEvent, index:Number):void {
    // ...
}

如果您稍后需要删除处理程序,您可以传递arguments.callee.

于 2012-07-06T22:56:39.947 回答