actionscript 3 - Access of undefined propety and other errors (AS3) -


ok trying work on making scripts external .as files. had them on timeline , worked fine when put them in file don't work @ all. code of right now

so got script working externally thank :) need addchild stage. use

import scripts.resources.main;  var main:main = new main;  addchild(main); 

in external script addchild in main function now.

package scripts.resources { import flash.events.event; import flash.events.mouseevent; import flash.net.urlrequest; import flash.net.urlvariables; import flash.net.urlloader; import flash.net.urlrequestmethod; import flash.events.outputprogressevent; import flash.display.sprite; import flash.display.movieclip; import flash.display.stage; import flash.events.eventdispatcher;   public class main extends eventdispatcher{      private var _stage:stage;     var url:string = "php/data_user.php";     //declare our graphic vars     var stmbar:resourcebar;     var tenbar:resourcebar;     var potbar:resourcebar;     var spdbar:resourcebar;     var crtbar:resourcebar;     var awrbar:resourcebar;     var resbar:resourcebar;     var statup:sprite;      //this returns number of milliseconds since midnight january 1, 1970     //tacking time variable @ end of php file locations ensure no caching     var now:date = new date();     var time:number = now.gettime();      /*     *  these should point php files. recommend not using http path because accessing     *  site via 'www.yoursite.com' versus 'yoursite.com' makes difference , cause undefined     *  issues. instead use relative path like: var registerlocation:string = "membership/php/register.php";     */      var registerlocation:string = "php/register.php" + "?" + time;     var loginlocation:string = "php/login.php" + "?" + time;     var editprofilelocation:string = "php/editprofile.php" + "?" + time;     var forgotpwlocation:string = "php/forgotpw.php" + "?" + time;     var statslocation:string = "php/user_stats.php" + "?" + time;     var statsgylph:string = "php/data_user.php" + "?" + time;      //userinfo object gets populated stores of users information.     var userinfo:object = new object();      //the minimum length of password.     var passwordlength:number = 4;      /*     *  var cookie refers swfobjects addvariable. if @ membership.php, swf     *  object defined, addvariable shows var called usercookie.     *  decide if program should remember users username or not.     */     //cookieusername username gets put user field if cookie has been set  public function main(stage:stage) {         _stage = stage;         var _loader:urlloader = new urlloader();         var _request:urlrequest = new urlrequest(url + "?id=" + userinfo.id);         _request.method = urlrequestmethod.get;         _loader.addeventlistener(event.complete, onloaddata);         _loader.load(_request);         _stage.addchild(stmbar);         stmbar.x = -50;         stmbar.y = 200;     }      public function myclass(){         this.addeventlistener(event.added_to_stage, addedtostage);       }      private function addedtostage(e:event):void {         this.removeeventlistener(event.added_to_stage, addedtostage);          //this code run after you've added object stage         //it equivalent of when timeline code runs     }         function onloaddata(e:event):void {             var str:string =  e.target.data;              var array:array =str.split("-");             for(var i:int;i < array.length; i++)                output(i+1,array[i]);         }       public function output(field:number,i:int):void          {             if (field == 5)             {             stmbar = new resourcebar(0, 0, i, "stamina", 0x990000);             stmbar.x = -200;             stmbar.y = 50;             }             else if (field == 6)             {             tenbar = new resourcebar(0, 0, i, "tenacity", 0xff9900);             tenbar.x = 200;             tenbar.y = 50;             }             else if (field == 7)             {             potbar = new resourcebar(0, 0, i, "potency", 0xcc3399);             potbar.x = -200;             potbar.y = 100;             }             else if (field == 8)             {             spdbar = new resourcebar(0, 0, i, "speed", 0x00cc00);             spdbar.x = 200;             spdbar.y = 100;             }             else if (field == 9)             {             crtbar = new resourcebar(0, 0, i, "crit", 0x009999);             crtbar.x = -200;             crtbar.y = 150;             }             else if (field == 10)             {             awrbar = new resourcebar(0, 0, i, "awareness", 0x3399cc);             awrbar.x = 200;             awrbar.y = 150;             }             else if (field == 11)             {             resbar = new resourcebar(0, 0, i, "resistance", 0x999999);             resbar.x = -200;             resbar.y = 200;              addeventlistener(event.enter_frame, onloop);             }         function onloop(e:event):void         {                    stmbar.change(1);             tenbar.change(1);             potbar.change(1);             spdbar.change(1);             crtbar.change(1);             awrbar.change(1);             resbar.change(1);         }     } }  } 

my problem won't add children in main function when addchild of main. able add child children inside it?

in class files, code needs wrapped in function. have:

        _loader = new urlloader();         _request = new urlrequest(url + "?id=" + (stage.parent movieclip).userinfo.id);         _request.method = urlrequestmethod.get;         _loader.addeventlistener(event.complete, onloaddata);         _loader.load(_request); 

you need place in function code valid. if want bit of code run right away, can place in constructor (that function in class runs when instantiate new keyword, eg new main(). if main document class, constructor first code run in application.

constructors public functions have exact name of class.

    public function main(){         _loader = new urlloader();         _request = new urlrequest(url + "?id=" + (stage.parent movieclip).userinfo.id);         _request.method = urlrequestmethod.get;         _loader.addeventlistener(event.complete, onloaddata);         _loader.load(_request);     } 

something keep in mind, in constructor of display objects, stage/parent/root keywords not have value, populated once display object has been added display. so, it's common use approach:

package myclass extends sprite {     public function myclass(){         this.addeventlistener(event.added_to_stage, addedtostage);     }      private function addedtostage(e:event):void {         this.removeeventlistener(event.added_to_stage, addedtostage);          //this code run after you've added object stage         //it equivalent of when timeline code runs     } } 

also, tip writing class files - follow convention of making class names start uppercase letter, , instances of classes start lowercase letters.


Comments

Popular posts from this blog

qt - Using float or double for own QML classes -

Create Outlook appointment via C# .Net -

ios - Swift Array Resetting Itself -