addressing a child from another class created by getdefinitionbyname
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:
You should stick to naming conventions: Class names should start with an upper case letter. So it should be
Page
instead ofpage
. Otherwise, you have a member variable with the same name as a type - and potential compilation errors.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 makepage
an instance ofPage
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 );
If you make
page
an instance of*
, as is implied above, you need to cast to thePage
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();
}
精彩评论