Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
827 views
in Technique[技术] by (71.8m points)

actionscript 3 - Getting data from custom classes (an OOP question)

How can I get some var / data from a custom classes?

The XML class

package classes
{
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.display.MovieClip;

    public class videoData extends MovieClip
    {   
       private var myXML:XML;
       private var myXMList:XMLList;

       public function videoData()
       {
          var myXMLLoader:URLLoader = new URLLoader();
          myXMLLoader.load(new URLRequest("playlist.xml"));
          myXMLLoader.addEventListener(Event.COMPLETE, processXML);
       }

       private function processXML(e:Event):void
       {
          myXML = new XML(e.target.data);
          myXMList = new XMLList(myXML.children());
       }

       public function getXML()
       {
          return myXML;
       }
    }
}

The class that is calling the XML

package classes
{
    import flash.display.MovieClip;
    import flash.events.MouseEvent;
    import classes.videoData;

    public class playList extends MovieClip
    {
       private var vData:videoData = new videoData();

       public function playList()
       {
          trace(vData.getXML())
       }
    }
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I would setup an event listener in 'playList' and dispatch an Event from 'videoData' once the XML has finished loading. That way you know when it's finished loading without using ENTER_FRAME (which will use alot more CPU as its checking every frame).

package classes
{
    import flash.events.*;
    import flash.display.MovieClip;
    import classes.VideoData;

    public class PlayList extends MovieClip
    {
       private var vData:VideoData;

       public function PlayList()
       {
           vData = new VideoData();
           vData.addEventListener(Event.COMPLETE, onXMLCompleteHandler);
       }

       private function onXMLCompleteHandler(e:Event):void
       {
           vData.removeEventListener(Event.COMPLETE, onXMLCompleteHandler);
           trace(vData.getXML());
       }
    }
}

package classes
{
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.display.MovieClip;

    public class VideoData extends MovieClip
    {   
       private var myXML:XML;
       private var myXMList:XMLList;
       private var myXMLLoader:URLLoader;

       public function VideoData()
       {
          myXMLLoader = new URLLoader();
          myXMLLoader.load(new URLRequest("playlist.xml"));
          myXMLLoader.addEventListener(Event.COMPLETE, processXML);
       }

       private function processXML(e:Event):void
       {
           myXMLLoader.removeEventListener(Event.COMPLETE, processXML);
           myXML = new XML(e.target.data);
           myXMList = new XMLList(myXML.children());
           dispatchEvent(e);
       }

       public function getXML():XML
       {
           return myXML;
       }
    }
}

You should also ALWAYS capitalise your class names 'VideoData' not 'videoData'


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...