actionscript 3 - Time taken by creating objects with JSON values in AS3 -


i have sentence in main.as file:

mytweet = new tweet(jsonarray[servicioelegido].url_json); trace(mytweet.lasttweet); 

this sentence pass string contrusctor url json, showed bellow:

package  {     import flash.display.movieclip;     import flash.events.mouseevent;     import flash.display.sprite;     import flash.events.event;     import flash.events.ioerrorevent;     import flash.net.urlloader;     import flash.net.urlrequest;     import flash.display.stagedisplaystate;     import com.adobe.serialization.json.json;      public class tweet {         public var urlmytweet:string; //url ak servicio de los tweets         public var numfollowers:int; //num de seguidores         public var numfollowed:int; //num de seguidos         public var numtweets:int; //num total de tweets         public var lasttweet:string; //ultimo tweet. texto         public var lasttweetret:int; //retweets del último tweet         public var lasttweetfav:int; //favs del último tweet         public var lasttweetfecha:string;//fecha del último tweet          public function tweet(_urltweet:string) {             this.urlmytweet = _urltweet;             var loader:urlloader = new urlloader();             var request:urlrequest = new urlrequest();             request.url = this.urlmytweet;             loader.load(request);             loader.addeventlistener(event.complete, onloadercompletetw);             loader.addeventlistener(ioerrorevent.io_error,informadorio_tw);         }          private function onloadercompletetw(e:event):void{             var loader:urlloader = urlloader(e.target);             var jsonarray:array = com.adobe.serialization.json.json.decode(loader.data);             this.numfollowers = jsonarray[0].user.followers_count;             this.numfollowed = jsonarray[0].user.friends_count;             this.numtweets = jsonarray[0].user.statuses_count;             this.lasttweet = jsonarray[0].text;             this.lasttweetret = jsonarray[0].retweet_count;             this.lasttweetfav = jsonarray[0].favorite_count;             this.lasttweetfecha = jsonarray[0].created_at;             trace(this.numfollowers);             }          //para gestionar los errores de io         public function informadorio_tw(e:event):void{             trace(e);         }      }  } 

the problem have don't know when main.as file accesses property of tweet class not obtain 0 in trace sentence.

i think problem it's regarded time taken constructor ask json (passed string parameter), not created when main.as file ask property of object.

any ideas?

that's indeed class that's not usable.

your best bet tear out functionality , loading of json externally (outside tweet class).

first of all, tweet data object holding list of properties.

  1. remove current constructor.
  2. move content of onloadercompletetw constructor. allow 1 parameter, json string, pass value of parameter json.decode().
  3. remove urlloaders , related code class
  4. your tweet class accepts json response , parses properties.
  5. create class twitter serves connection online api, urlloader related things in it, dispatch events when json received , parse information tweet object.

a possible usage (untested pseudo code):

var twitter:twitter = new twitter(); twitter.addeventlistener(tweetevent.received, ontweet);  function ontweet(e:tweetevent):void {     trace(e.tweet.numtweets); } 

twitter around while. it's advisable use existing library twitter as3


Comments

Popular posts from this blog

python - Healpy: From Data to Healpix map -

c - Bitwise operation with (signed) enum value -

xslt - Unnest parent nodes by child node -