A Flash Developer Resource Site

Results 1 to 8 of 8

Thread: AS2 Class Instance Delete

Hybrid View

  1. #1
    Senior Member
    Join Date
    Apr 2001
    Location
    UK
    Posts
    493

    AS2 Class Instance Delete

    How do you delete an Object instance created from a Class?

    Below is the class from TestDestroy.as

    Code:
    class TestDestroy extends Key
    {
    	public var myListen:Object
    	public var aNumber:Number
    	public var aClip:MovieClip
    	
    	function TestDestroy(clip:MovieClip)
    	{
    		this.aClip=clip
    		this.aNumber=10
    		this.myListen=new Object()
    		
    		Key.addListener(this.myListen)
    		
    		this.myListen.onKeyDown=this.killME
    	}
    	
    	public function killME():Void
    	{
    		trace("kill")
    		delete this		
    	}
    	
    	
    }

    Now in the flas movie i have

    Code:
    var destroyTest:TestDestroy=new TestDestroy(_root.Ball)
    
    trace("destroyTest:"+destroyTest)
    The trace returns undefined but i can access the Object properties etc.. hence the aNumber and aClip additions.

    i cant delete the Object instance from inside the class or from the main timeline.

    Obviously it cant find the object as it is undefined, but list the objects when published and its there!.

    Very odd

    I am transfering to AS2 from AS1 and may have missed something but it is quite confusing that and object instance can exist but cant be destroyed, but can be referenced!

    Any pointers gratefuilly recieved

    Cheers
    JOn

  2. #2
    Member Pepember
    Join Date
    Jul 2001
    Location
    Berlin
    Posts
    886
    what about:
    trace("destroyTest:"+(typeof destroyTest));
    if you want to delete it:

    delete destroyTest;
    Please sign here

  3. #3
    Senior Member
    Join Date
    Apr 2001
    Location
    UK
    Posts
    493
    thanks for the reply.

    Code:
    trace("destroyTest:"+(typeof destroyTest));
    does give me an Object trace and

    Code:
    delete destroyTest;
    does clear the object ( i needed to delete the Key listener too)

    However:

    Code:
    destroyTest.KillME()
    Doesnt do anything.

    There must be a way of using a class Method to delete / destroy the Object instance ...isnt there?


    I have attached a zip file with the class .as file and fla.
    I have commented the file a little so you can see what i am doing, and tehre is an UPDATE method called from the main timeline to see if the Object instance is still there.

    Hopefully the files will make more sense than my ramblings

    Thanks again and i hope you (or anyone else) can give me some pointers

    Cheers
    JOn
    Attached Files Attached Files

  4. #4
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    Quote Originally Posted by Jayhoo
    There must be a way of using a class Method to delete / destroy the Object instance ...isnt there?
    Not really, no - Flash will only delete an object if no references to it remain anywhere in memory...and since you create a reference that exists outside the class when you instantiate it, you won't be able to delete the object from within the class itself...

    K.

  5. #5
    Senior Member
    Join Date
    Apr 2001
    Location
    UK
    Posts
    493
    ok, thanks for the reply.

    Many work arounds to this but would be nice and tidy if it was all encapsulated in the class.....


    thanks again
    JOn

  6. #6
    Registered Deviant
    Join Date
    Sep 2004
    Location
    Cornelius, OR, USA
    Posts
    280
    The easiest work around is to not store your new object in a variable when creating it.
    Code:
    //Something like this
    new OneShot(...);
    //Instead of this
    //var x:OneShot = new OneShot(...);
    The only catch to it, is unless you assign the property _parent to something inside of your class, there is no way to locate it anymore other than providing some feedback function. I just happen to be working on a class that encapsulates the MoveClipLoader and a listener object in to one neat bundle (aptly named oneshot) that does exactly this. The intent is that one can simply pass in a URL and the class will then create a new empty movie clip, load the URL and then destroy itself. It also uses two callback functions for onLoadError() and onLoadInit() so one can determine when a moveclip fails to load or when it completes. I can post the class and an example once I work out the kinks in the callback functions if anyone is interested.
    If you can read this, you're in the right place.

  7. #7
    Senior Member
    Join Date
    Apr 2001
    Location
    UK
    Posts
    493
    cheers for the pointes mate.

    I would be interseted in seeing the class when it is finished if you are good to share it

    Thanks again

    Jon

  8. #8
    Registered Deviant
    Join Date
    Sep 2004
    Location
    Cornelius, OR, USA
    Posts
    280
    Sorry it's taken me so long to reply, work got in the way. I've commented just about every line in the class file so hopefully you can follow along with what's happening. The FLA is in MX2004 format and contains a few different examples. When running the SWF in the design environment, go to Debug->List Variables to see what's happening. Note how when you assign OneShot to a variable it stays behind (but the MovieClipLoad object and the listener are successfully deleted!), however, when you use OneShot without saving the class to a variable, it vanishes from memory when it's done.

    From the OneShot.as file
    Class OneShot
    -------------
    Loads a specific URL in to a new MovieClip and deletes itself when finished (if possible). Intended for use
    as a quick way to load one specific image of SWF where a MovieClipLoader is needed, but the re-usability is not.

    Usage: new OneShot( url, [parent], [mc_name], [depth], [successFunction], [failureFunction] );

    Options in [] are optional and will be calculated by the following rules:
    parent - If nothing is passed, _root is assumed
    depth - If nothing is passed, calls parent's getNextHighestDepth() function
    mc_name - If nothing is passed, the name will be oneshot + "parent's name" + depth, e.g. oneshotmcBall1000
    successFunction - If nothing is passed, no call back is available
    failureFunction - If nothing is passed, no call back is available

    successFunction will receive the targetMC when the MovieClipLoader enters the onLoadInit function
    -- after movie is loaded, and first scene is displayed
    failureFunction will receive three parameters: targetMC, ErrorCode (string), HTML Error Code (number)

    Granted, using OneShot is not as flexible as the MovieClipLoader object, but can be used for a quick, one-time load
    where error checking is not necessarily needed. The OneShot object *does not* provide feedback mechanisms for
    watching the loading progress; wrapping all the features of the MovieClipLoader object defeats the purpose.

    This class is intended primarily as a learning resource. It's functionality is not designed to replace any
    part of the Flash ActionScript language. Use at your own risk.
    Please note, this is more a proof of concept work than anything actually useful. If you can find use for it, by all means, please use it.
    Attached Files Attached Files
    If you can read this, you're in the right place.

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