This is my code on button click. It adds myView to the container. And takes care of fact that all previous instances get removed before adding again. I wanna know, if there is some better way of making this “check” ? I especially wanna have some way to remove the 1s 7 lines of this code ( within comments). It seems like an unrelated block of code in the normal code-flow.
function button_CLICK(e:Event)
{
///////////////////////////////////////////////////
if ( myView!= null)
{
if ( contains(myView))
{
removeChild(myView) ;
}
}
/////////////////////////////////////////////////////
myView = new myView("hello")
addChild(myView);
}
it would depend on what the object
myViewconsists of. if, for example, it has its own event listeners you would want to use the condition check to clean up the object before disposing of it so there are no memory leaks.however, if
myViewis a simple display object that doesn’t contain any leakable references or event listeners than you could simply reassign the variable to overwrite the previous.keep in mind that
addChild()places the added display object on top above all others. but regardless if that’s something you’d like to avoid or not, i believe it would be better to assign a default or empty view once instead ofaddChild(myView)each time the button is clicked.i assume your code is for for demonstration because
new myView("hello")is going to always be the same object? unless perhaps this object relies on dates or timers to make instances different from previous constructs.