A Flash Developer Resource Site

Results 1 to 10 of 10

Thread: Access Class object in .fla

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    7

    Access Class object in .fla

    Hello all. I have tried for days to solve this. I am trying to access the "newsData" object from my .fla to populate my manually created textfields.

    Can someone help me access the object?

    Here is the class:
    package {

    import com.adobe.serialization.json.JSON;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;

    public class News {
    public var newsRequest:URLRequest = new URLRequest();
    public var newsLoader:URLLoader = new URLLoader();
    public static var newsData:Object = new Object;

    public function News(url){
    newsRequest.url = (url);
    newsLoader.addEventListener(Event.COMPLETE, getNewsData);
    newsLoader.load(newsRequest);
    }

    public function getNewsData(e:Event):void{
    newsData = JSON.decode(newsLoader.data);
    trace(newsData.results[4].name);
    }
    }
    }

  2. #2
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    Actionscript Code:
    package {
        //import com.adobe.serialization.json.JSON;
        import flash.net.URLLoader;
        import flash.net.URLRequest;
        import flash.events.Event;
        import flash.events.EventDispatcher;

        public class News extends EventDispatcher{
            public var newsRequest:URLRequest;
            public var newsLoader:URLLoader;
            public var newsData:Object;

            public function News(url) {
                newsData=new Object();
                newsRequest=new URLRequest(url);
                newsLoader=new URLLoader();
                newsLoader.addEventListener(Event.COMPLETE,getNewsData);
                newsLoader.load(newsRequest);
            }

            public function getNewsData(e:Event):void {
                //newsData=JSON.decode(newsLoader.data);
                //trace(newsData.results[0].name);
                newsData=(e.target.data);
                dispatchEvent(new Event(Event.COMPLETE));
            }
        }
    }

    Usage:

    Actionscript Code:
    import News;
    var news:News=new News("newsData.xml");
    news.addEventListener(Event.COMPLETE,eventCompleteListener);

    function eventCompleteListener(evt:Event):void {
        var xml:XML=new XML(evt.target.newsData);
        trace(xml)
    }



    arkitx
    Last edited by arkitx; 03-13-2012 at 02:14 AM.

  3. #3
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    Thanks arkitx. It works perfectly.

  4. #4
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    A Document Class version:

    Actionscript Code:
    <?xml version="1.0" ?>
    <newsData name="Main News">
          <news name="This News"></news>
    </newsData>

    Actionscript Code:
    package {
        //import com.adobe.serialization.json.JSON;
        import flash.display.MovieClip;
        import flash.net.URLLoader;
        import flash.net.URLRequest;
        import flash.events.Event;
        import flash.text.TextField;

        public class NewsDoc extends MovieClip {
            public var newsRequest:URLRequest;
            public var newsLoader:URLLoader;
            public var newsData:XML;
            private var url:String="newsData.xml";
            public function NewsDoc() {
                newsRequest=new URLRequest(url);
                newsLoader=new URLLoader();
                newsLoader.addEventListener(Event.COMPLETE,getNewsData);
                newsLoader.load(newsRequest);
        }
            public function getNewsData(e:Event):void {
                //newsData=JSON.decode(newsLoader.data);
                //trace(newsData.results[0].name);
                newsData=new XML(e.target.data);
                output_txt1.text=newsData.@name;
                output_txt2.text=new XML(e.target.data);
            }
        }
    }


    arkitx

  5. #5
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    Thanks again arkitx. Your code works perfectly. I modified it to output json.

    I still have to decode the json file in my .fla script. Can you tell me how I can obtain the "newsData" object after json decoding and population in the class script and make its contents available in the .fla?

    My objective is to be able to call the class (from the .fla) like this:

    Actionscript Code:
    var news1:News = new News('http://myjsonfeed.com')

    and return the 'newsData' object so I can use the information in my manually created textFields:

    Actionscript Code:
    myText.text = News.newsData.results[0].name

  6. #6
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    Could you post your project?

    arkitx

  7. #7
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    This is an effort to improve a previous project. I can't post that project because the newsFeed is unavailable outside my network.

    My original project was a fully frame-scripted (messy) application where I imported and decoded a json news feed. In the original project I populated my textfields within a function:

    Actionscript Code:
    import com.adobe.serialization.json.JSON;
        import flash.net.URLLoader;
        import flash.net.URLRequest;
        import flash.events.Event;

           var newsRequest:URLRequest;
           var newsLoader:URLLoader;
           var newsData:Object;

            function News(url) {
                newsData=new Object();
                newsRequest=new URLRequest("myurl.com");
                newsLoader=new URLLoader();
                newsLoader.addEventListener(Event.COMPLETE,getNewsData);
                newsLoader.load(newsRequest);
            }

            function getNewsData(e:Event):void {
                newsData=JSON.decode(newsLoader.data);

    //Populate textFields://////
                txt.Text = newsData.name[0].name
                txt2.Text = newsData.name[1].name
               
            }

    I'm trying to create a class that decodes the json and makes the data in the "newsData" variable available to the .fla and other classes. So after the class "News" is called:

    Actionscript Code:
    import com.adobe.serialization.json.JSON;
        import flash.net.URLLoader;
        import flash.net.URLRequest;
        import flash.events.Event;
       
        public class News {
            public var newsRequest:URLRequest;
            public var newsLoader:URLLoader;
            public var newsData:Object;

            public function News(url) {
                newsData=new Object();
                newsRequest=new URLRequest(url);
                newsLoader=new URLLoader();
                newsLoader.addEventListener(Event.COMPLETE,getNewsData);
                newsLoader.load(newsRequest);
            }

            public function getNewsData(e:Event):void {
                newsData=JSON.decode(newsLoader.data);
               
            }
        }

    ...the populated "newsData" variable is available elsewhere:

    Actionscript Code:
    trace (newsData.length);
    trace (newsData.name[0].name);

    Basically trying to make the variable within a function accessable.

    Thanks,
    JV
    Last edited by jtvalles; 03-16-2012 at 03:36 PM.

  8. #8
    Senior Member
    Join Date
    Jan 2011
    Posts
    171
    Actionscript Code:
    public var newsData:Object;


    public function getNewsData(e:Event):void {
                var xml:XML=new XML(e.target.data);
                var list:XMLList=xml.children();
                newsData=new Object();

                newsData={name:(xml.@name)};
                for (var i=0; i<list.length(); i++) {
                    newsData[i]={name:(list[i].@name),news:(list[i])};
                }
                trace(newsData.name);
                trace(newsData[0].name);
                trace(newsData[0].news);
    }



    arkitx

  9. #9
    Registered User
    Join Date
    Mar 2012
    Posts
    1
    I've had a similar problem and this works perfectly for me.



  10. #10
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    I went back to your first post and:

    Actionscript Code:
    package {
    import com.adobe.serialization.json.JSON;
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.events.Event;
    import flash.events.EventDispatcher;

    public class News extends EventDispatcher{
    public var newsRequest:URLRequest;
    public var newsLoader:URLLoader;
    public var newsData:Object;

    public function News(url) {
    newsData=new Object();
    newsRequest=new URLRequest(url);
    newsLoader=new URLLoader();
    newsLoader.addEventListener(Event.COMPLETE,getNews Data);
    newsLoader.load(newsRequest);
    }

    public function getNewsData(e:Event):* {
    newsData=JSON.decode(newsLoader.data);
    dispatchEvent(new Event(Event.COMPLETE));
    }
    }
    }
    Usage:


    Actionscript Code:
    import flash.events.MouseEvent;
    import flash.events.Event;
    import News;

    var search1:News = new News("https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=500&types=food&name= harbour&sensor=false&key=AIzaSyB_u-ehd2WBol6-JqZjxmaQoSc2S3y75XY");
    var search2:News = new News("https://maps.googleapis.com/maps/api/place/search/json?location=-33.8670522,151.1957362&radius=5000&types=food&name =inn&sensor=false&key=AIzaSyB_u-ehd2WBol6-JqZjxmaQoSc2S3y75XY");
    var json:Object = new Object;
    var json2:Object = new Object;

    search1.addEventListener(Event.COMPLETE,eventCompl eteListener);

    function eventCompleteListener(evt:Event):void {
    json = (evt.target.newsData);
    trace(json.results[0].name);
    }

    search2.addEventListener(Event.COMPLETE,eventCompl eteListener2);

    function eventCompleteListener2(evt:Event):void {
    json2 = (evt.target.newsData);
    trace(json2.results[0].name);
    }

    stage.addEventListener(MouseEvent.CLICK, getNews);

    function getNews(event:MouseEvent):void {
    news1_txt.text = json.results[0].name;
    news2_txt.text = json2.results[0].name;
    }

    Thanks a ton.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center