A Flash Developer Resource Site

Results 1 to 1 of 1

Thread: [F8] HOWTO: Removing/destroying a v2 component that was created in actionscript.

  1. #1
    Unaffiliated deity.
    Join Date
    Sep 2000
    Location
    Tallahassee, FL
    Posts
    83

    [F8] HOWTO: Removing/destroying a v2 component that was created in actionscript.

    I literally spent over 20 hours playing with variations on code and scouring the web, the (overly-obtuse) flashcoders newsgroup and the (utterly reprehensible) Flash docs for instructions on using the DepthManager class to properly destroy component instances. It was a freaking nightmare, but at last I've discovered the way to do it. I will now give myself a round of applause for figuring out something that should have taken a couple of minutes if the people writing the flash docs would ever get their **** together. Anyway, here's the scoop.

    Let's first do an example on the _root timeline and then I will show you how to do it from within a custom Class, which is what really messed me up.

    Example on the root timeline:
    (warning: this forum is inserting spaces in the code below and there's nothing I can do about it...)
    Code:
    //put this on the first frame of your _root timeline
    import mx.managers.DepthManager;
    progbar = DepthManager.createClassObjectAtDepth(mx.controls.ProgressBar,DepthManager.kTooltip,{label:"Connecting..."});
    progbar._name = "progbar";
    progbar.indeterminate = true;
    progbar.setSize(300,100);
    progbar.move(Stage.width/2 - progbar.width / 2 , Stage.height/2 - progbar.height/2);
    
    //put this on a following frame of your _root timeline
    destroyObject(progbar._name);
    Notice that since the DepthManager class doesn't let you specify a name for the new instance, you have to manually give it one after it is created. Why? Because the stupid, stupid, stupid destroyObject() method only takes a string name of the object instance and will not let you simply pass it an object reference like a non-retarded language would. You may also notice that I'm creating the object by specifically calling a method of the DepthManager class. Despite the myriad of examples posted on the Flashcoder newsgroup, you cannot simply call createClassObjectAtDepth() as if it were a method of your _root movie or any other movie.

    Okay, so that wasn't so bad (unless you read the MM docs first and tried to follow their example). But now...

    Lets try it from inside a custom Class where you don't have access to the destroyObject() method in your _root timeline.

    Code:
    class myClass
    {
    
    	public var progbar:Object;
    	
    	
    	function myClass(){
    		//lets assume you wanted to create a progress bar in your constructor
    		//and then destroy it in another function called killProgressBar()
    		
    		this.progbar = DepthManager.createClassObjectAtDepth(mx.controls.ProgressBar,DepthManager.kTooltip,{label:"Connecting..."});
    		this.progbar._name = "progress_bar";
    		this.progbar.indeterminate = true;
    		this.progbar.setSize(300,100);
    		this.progbar.move(Stage.width/2 - this.progbar.width / 2 , Stage.height/2 - this.progbar.height/2);
    	}
    	
    	function killProgressBar(){
    		this.progbar._parent.destroyObject(this.progbar._name);
    	}
    	
    }
    Notice this time, that we could not kill the progress bar simply by calling destroyObject(this.progbar._name). We do not have access to the destroyObject method like we did on the _root timeline. We must use an object that has access to that method, so we call the parent timeline of wherever the progbar object ended up. I'm assuming this code works because the component is being created in the reserved kTooltip depthspace of the _root timeline, but I'm not positive.

    So is it just me or is it totally ****ing retarded to instantiate components with a function that doesn't let you provide an instance name, when you are definitely going to need an instance name to destroy or remove the component. The answer is yes, it is totally ****ing retarded.

    I will never understand why the hell the people working on the MM docs don't bother to include examples exactly like the ones I've just done here, but they don't. All I know is that this works and it took me a helluva long time to figure out. Hopefully it will save some people a ton of aggravation.
    Last edited by minorgod; 09-12-2006 at 12:50 AM.
    Nowhere does science promise emancipation.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center