actionscript 3 - AS3 Flash- Adding multiple different movieclips to stage from one class -


i've searched everywhere , can't find answer question , i'll best explain it.

i have 2 movieclips in library, 'bluknife' , 'cat' (it's rpg game.). both movieclips linked as3 classes of same names.

i know can can add both of these items stage having following code in document class:

var knife:bluknife = new bluknife(); stage.addchild(knife);  knife.x = stage.stagewidth / 2; knife.y = stage.stageheight / 2;  var ct:cat = new cat(); stage.addchild(ct);  ct.x = stage.stagewidth / 2; ct.y = stage.stageheight / 2; 

however don't want stuff in document class, made class want use call of stuff stage , named callitems:

package  {      import flash.display.movieclip;      import flash.display.movieclip;     import flash.display.stage;       public class callitems extends movieclip     {           public function callitems()         {             var knife:bluknife = new bluknife();              stage.addchild(knife);              knife.x = stage.stagewidth / 2;             knife.y = stage.stageheight / 2;              var ct:cat = new cat();              stage.addchild(ct);              ct.x = stage.stagewidth / 2;             ct.y = stage.stageheight / 2;         }      }  } 

i'm wondering how go calling callitems class document class? i've tried few things, creating instance of callitems , adding stage document class (which wasn't surprised didn't work). here's function:

public function engine()         {             var calling:callitems = new callitems();             stage.addchild(calling);          } 

i got following error: typeerror: error #1009: cannot access property or method of null object reference. @ callitems() @ engine()

the name of document class engine

first, have misconception, when create callitems (note in actionscript class names should in camel case initial capital leter), somehow classes callitems uses not compiled class creates callitems instance. every class directly referenced class uses gets compiled in.

to resolve that, need program interfaces, , have document class know interface. there 2 ways know of populate variables of interface type without having document class have reference implementation.

  1. put items on stage in ide, , give them instance names match variables used, , assign base class implements interface. note class still compiled swf, because it's used in swf, actionscript code not have direct knowledge of it.
  2. load in external swf. implementation compiled other swf.

now, actual problem. callitems instance adding stage, rather itself. since has not yet been added stage (and i'd recommend engine add if you're going go through tiresome , tedious process of creating , adding through code) rather adding stage.

display objects not have reference stage until have been added on display list.

you may want consider why you're adding calling in engine, since presumably think calling adding things makes stage (except doesn't have reference stage).


Comments

Popular posts from this blog

java - Jmockit String final length method mocking Issue -

asp.net - Razor Page Hosted on IIS 6 Fails Every Morning -

c++ - wxwidget compiling on windows command prompt -