ActionScript:如何将动态加载的地址添加到动态加载菜单上的条目中?

时间:2012-07-06 18:29:45

标签: xml actionscript-3 flash

所以我有一个菜单,其中包含从XML文件动态加载的选项,效果非常好。他们都为MouseOvers附加了eventListener,这也很好。我遇到问题的地方是我需要为每个都添加一个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。

1 个答案:

答案 0 :(得分: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