开发者

addressing a child from another class created by getdefinitionbyname

开发者 https://www.devze.com 2023-02-02 20:20 出处:网络
I have a class "pagecompiler" which does the following: if(page){ removeChild(page); } switch (appModel.currentPage){

I have a class "pagecompiler" which does the following:

if(page){
                removeChild(page);
            }
            switch (appModel.currentPage){
                case "Programma":
                case "Winkelwagen":
                case "Films":
                case "Contact":
                case "Reserveer":
                    var pageClass:* = getDefinitionByName("be.reynaertvincent.view.pages."+appModel.currentPage+"Page");
                    page = new pageClass();
                    addChild( page );
                    break;
}

so it creates a page depending on the switch, the names are (contactpage, filmspage, contactpage, etc.)

each of these pages extend from a class called "page".

And in "page" class I do the following:

contentBg = new ContentBg();
sidebarBg = new SidebarBg();
addChild(contentBg);
addChild(sidebarBg);

Now what I would like is to apply a tween on contentBg when I do the following in pagecompiler开发者_StackOverflow中文版:

if(page){
   removeChild(page);
}

but I can't seem to address contentBg from there. I tried:

if(page.contentBg){
   tweenlite.to(page.contentBg,blablabla);
   //removeChild(page);
}

but it doesn't get recognized. Anyone having any ideas?


I see a number of problems with your code. If you correct them, your problem should be solved:

  1. You should stick to naming conventions: Class names should start with an upper case letter. So it should be Page instead of page. Otherwise, you have a member variable with the same name as a type - and potential compilation errors.

  2. getDefinitionByName() is a costly way of instantiating a class in terms of performance, and since it also isn't type safe, you are dealing with a potential problem. Since you already know which classes you are going to instantiate, why not just make page an instance of Page and extend your switch statement:

    private var page:Page;
    
    
    // some code here
    
    
    switch (appModel.currentPage){
        case "Programma":
            page = new ProgrammaPage();
            break;
        case "Winkelwagen":
            page = new WinkelwagenPage();
            break;
        case "Films":
            page = new FilmsPage();
            break;
        case "Contact":
            page = new ContactPage();
            break;
        case "Reserveer":
            page = new ReserveerPage();
            break;
        }
    addChild( page );
    
  3. If you make page an instance of *, as is implied above, you need to cast to the Page class in order to access its contentBg property. Something like this:

    tweenlite.to (Page(page).contentBg, ....);
    

This last point, however, should already be solved if you follow the first two hints, since the compiler now knows that page is of type Page. It is also a nice example why the lower case naming mentioned above doesn't work, as page(page).contentBg is obviously ambiguous.


In your page class , you could create a public method:

  public function removeFromStage():void
  { 
       TweenLite.to ( contentBg , etc...   {.....  onComplete: remove})
  }

  //Called when the Tween is complete
  private function remove():void
  {
      parent.removeChild( this );

      //may come handy , don't forget to add a listener if you do this :)
      dispatchEvent ( new Event ( Event.COMPLETE ) );
  }

Then you can call it like this

if(page != null ){
   //add Complete event listener if necessary...
   page.removeFromStage();
 }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号