Rich Rodecker’s blog on flash, flex, actionscript, javascript, and php, with a dash of randomness
ReferenceLocator – store a reference to anything as a string
I was in a situation where i needed to refer to an instance of a Tween in a separate method than where it was created. The first two thoughts that come to mind are storing it as a var in the class, or attaching it to a property to an object(usually a dynamic property of a movieclip). That’s always bothered me a little cause its a hack…granted that’s the bread and butter of AS1, but there’s a reason people are moving away from that.
I dawned on me how many other times i need to store references somewhere for other things…for example, cases where I need to store a reference to the function returned from a Delegate.create() in order to delete it later or something.
So, i just whipped together this ReferenceLocator class (a little nod to ARP). basically you just pass an object to its addReference() method that contains two properties: referenceName and referenceTo. Optionally, you can add a method to the object named overwrite, and set it to true, and that will overwrite an existing reference with the same name. To get the object, you just pass its referenceName to the getReference() method. There’s also a deleteReference() method, which will delete the reference with the given name, optionally deleting the object as well.
You can read more about and download it from the downloads page.
| Print article | This entry was posted by rich on July 14, 2006 at 4:16 pm, and is filed under flash. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
about 3 years ago
yeah…so the more I’ve thought about this the more I realize this is pretty much just a Singleton replacement for the _global object, which a few extra methods.