如何将动态URL从XML传递到ActionScript 3中的onClick鼠标事件?

时间:2008-12-15 23:44:44

标签: xml flash actionscript-3 actionscript

我正在加载XML,我能够将XML节点读入我的flash中的文本字段。它也加载了URL,但是循环中的最后一个。它没有加载我点击的那个。我尝试使用event.target,但这不起作用。我非常接近搞清楚,我只是不确定在哪里看。

// loads xml 
var xml:XML = new XML();
var loader:URLLoader = new URLLoader();
loader.load(new URLRequest(audioPlaylist));
loader.addEventListener(Event.COMPLETE, onComplete);

function onComplete(evt:Event):void {
        xml = XML(evt.target.data);
        xmlList = xml.children();
        trace(xmlList);
        trackLength = xmlList.children().children().length();
        trace(trackLength);

        for(var i:int = 0; i < trackLength; i++) {
            trace(i);
            var track:Playlist_item = new Playlist_item();
            track.y = i * 28;

            track.playlist_text.text = xmlList.children().track[i].toString();
            trackURL = xmlList.children().track[i].@rel.toString();

            trace(trackURL);

            playlist_container.addChild(track);
            track.buttonMode = true;
            track.mouseChildren=false;

            track.addEventListener(MouseEvent.MOUSE_OVER, onCarHover);
            track.addEventListener(MouseEvent.MOUSE_OUT, onCarOut);
            track.addEventListener(MouseEvent.CLICK, onClickLoadData);

        }

}

function onCarHover(event:MouseEvent):void {
    event.target.gotoAndStop(6);
}

function onCarOut(event:MouseEvent):void {
    event.target.gotoAndStop(10);
}

function onClickLoadData(event:MouseEvent):void {
    ns.play(trackURL);
}

我越来越近了,我设法创建了一个带有索引值的数组 - 所以现在我可以从数组中选择不同的URL来播放,但我仍然不确定如何定位我点击的那个直接参加比赛。

这是我更新的代码:

// xml variables
var xmlList:XMLList;
var trackLength:Number;
var trackURL;
var trackNum:Number = -1;
var tracksArray:Array = new Array();


// loads xml 
var xml:XML = new XML();
var loader:URLLoader = new URLLoader();
loader.load(new URLRequest(audioPlaylist));
loader.addEventListener(Event.COMPLETE, onComplete);

function onComplete(evt:Event):void {
        xml = XML(evt.target.data);
        xmlList = xml.children();
        trace(xmlList);
        trackLength = xmlList.children().children().length();

        while (trackNum < trackLength) {
            trackNum = trackNum + 1;
            trace(trackNum);

            var track:Playlist_item = new Playlist_item();
            track.y = trackNum * 28;
            playlist_container.addChild(track);

            track.buttonMode = true;
            track.mouseChildren=false;

            track.playlist_text.text = xmlList.children().track[trackNum].toString();
            //trackURL = xmlList.children().track[trackNum].@rel.toString();

            tracksArray[trackNum] = xmlList.children().track[trackNum].@rel.toString();

            track.addEventListener(MouseEvent.MOUSE_OVER, onCarHover);
            track.addEventListener(MouseEvent.MOUSE_OUT, onCarOut);
            track.addEventListener(MouseEvent.CLICK, onClickLoadData);

        }   

}

function onCarHover(event:MouseEvent):void {
    event.target.gotoAndStop(6);
}

function onCarOut(event:MouseEvent):void {
    event.target.gotoAndStop(10);
}

function onClickLoadData(event:MouseEvent):void {

    trace(tracksArray[5]);

    trace(event.target.trackNum);
    ns.play(tracksArray[5]);
}

2 个答案:

答案 0 :(得分:1)

在我看来,你自己也很努力。我可能不正确,因为很难遍历您提供的代码。 XML播放列表的概念和单击显示对象来播放声音非常简单,我将演示。

XML

<?xml version="1.0" encoding="utf-8"?>
<xml>
    <item title="Song 1" path="audio/song1.mp3" />
    <item title="Song 2" path="audio/song2.mp3" />
    <item title="Song 3" path="audio/song3.mp3" />
    <item title="Song 4" path="audio/song4.mp3" />
    <item title="Song 5" path="audio/song5.mp3" />
    <item title="Song 6" path="audio/song6.mp3" />
</xml>

ActionScript 3代码

package
{
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.net.URLLoader;
    import flash.net.URLRequest;

    public class DocumentClass extends Sprite
    {
        private var _urlLoader:URLLoader;
        private var _urlRequest:URLRequest;
        private var _xml:XML;
        private var _xmlList:XMLList;

        public function DocumentClass():void
        {
            _urlLoader = new URLLoader();
            _urlRequest = new URLRequest();
            _urlRequest.url = 'path/to/playlist.xml';

            _urlLoader.addEventListener(Event.COMPLETE, onXMLLoaded);
            _urlLoader.load(_urlRequest);
        }
        private function onXMLLoaded(e:Event):void
        {
            _xml = new XML(e.target.data);
            _xmlList = new XMLList(_xml.item);

            //We use the index in the XML object as its ID. (The XML object/List is an array);
            for(var i:int = 0; i < _xmlList.length(); i++)
            {
                var s:MovieClip = new MovieClip();
                addChild(s);
                s.mouseChildren = false;

                var tf:TextField = new TextField();
                tf.text = _xmlList[i].@title;
                tf.y = i * 12 + 20; //Seperates the textfields by 12 px starting at y:20;
                s.path = _xmlList[i].@path;
                s.addChild(tf);
                s.addEventListener(MouseEvent.MOUSE_DOWN, onSDown);
            }
        }
        private var onSDown(e:MouseEvent):void
        {
            var s:Sound = new Sound(new URLRequest(e.target.path));
            s.play();
        }
    }
}

因此我们将XML加载到我们的文档类中。我们循环遍历xml length();很多时候,XML对象中有多少个孩子。对于每个循环或子项,我们创建一个MovieClip,向其添加一个文本字段,标记文本字段,然后将“path”(xmlList [int]。@ path)的属性添加到包含的movieclip中,其值为当前值来自xml的childs path属性。 MouseEvent被认为是MovieClip,我们告诉里面的文本字段不接收鼠标输入。

我希望这有助于将文本包装在MovieClip中,我们可以为它添加属性,可以通过目标访问事件方法,这是s。

答案 1 :(得分:0)

我找到了解决问题的方法!

我的解决方案是创建一个数组,按trackNum存储所有URL,然后在while循环中创建实例名称。然后,我使用带有子串的event.target.name来修剪文本,只留下数字。它很棒!

// xml variables
var xmlList:XMLList;
var trackLength:Number;
var trackURL;
var trackNum:Number = -1;
var tracksArray:Array = new Array();


// loads xml 
var xml:XML = new XML();
var loader:URLLoader = new URLLoader();
loader.load(new URLRequest(audioPlaylist));
loader.addEventListener(Event.COMPLETE, onComplete);

function onComplete(evt:Event):void {
        xml = XML(evt.target.data);
        xmlList = xml.children();
        trace(xmlList);
        trackLength = xmlList.children().children().length();

        while (trackNum < trackLength) {
            trackNum = trackNum + 1;

            var track:Playlist_item = new Playlist_item();
            track.y = trackNum * 28;
            playlist_container.addChild(track);
            track.name = "track" + [trackNum];

            trace(track);

            track.buttonMode = true;
            track.mouseChildren=false;

            track.playlist_text.text = xmlList.children().track[trackNum].toString();
            //trackURL = xmlList.children().track[trackNum].@rel.toString();

            tracksArray[trackNum] = xmlList.children().track[trackNum].@rel.toString();

            track.addEventListener(MouseEvent.MOUSE_OVER, onCarHover);
            track.addEventListener(MouseEvent.MOUSE_OUT, onCarOut);
            track.addEventListener(MouseEvent.CLICK, onClickLoadData);

        }   

}

function onCarHover(event:MouseEvent):void {
    event.target.gotoAndStop(6);
}

function onCarOut(event:MouseEvent):void {
    event.target.gotoAndStop(10);
}

function onClickLoadData(event:MouseEvent):void {

    //trace(tracksArray[5]);

    trace(event.target.name.substr(5));

    ns.play(tracksArray[event.target.name.substr(5)]);
}