Dynamic Flash

Confessions of a serial code abuser
  • rss
  • Home
  • MTASC
  • Archives
  • About me
  • Goodies
    • Base64 encoder/decoder class
  • My Bookshelf
  • My Talks

Actionscript 2.0 vs Object.addProperty()

Monday, 14 February 2005

Over the past 6 months or so I have been utterly consumed by the task of designing and building our content management system. The administration interface is written entirely in Actionscript 2.0, and I have learned to love that language like a brother* - I don't think I'll ever touch a line of Actionscript 1 again if I can help it.

In the administration application, we have a data class that represents a page that has an extensible collection of 'properties' (as we call them in the system - sorry about the confusing nomenclature) along with methods to allow properties to be added. The class also has a number of regular private properties, along with Actionscript 2.0 style getter / setter methods for those properties.

A super-simplified version of this class might look something like this:

PLAIN TEXT
Actionscript:
  1. class Page {
  2.     private var __properties:Array;
  3.  
  4.     private var __title:String;
  5.     private var __path:String;
  6.  
  7.     public function get title():String { return __title; }
  8.     public function get path():String { return __path; }
  9.  
  10.     public function Page(Void) {
  11.         __properties = new Array();
  12.     }
  13.  
  14.     public function addProperty(prop:Property) : Void {
  15.         __properties.push(prop);
  16.     }
  17. }

While the above may look fine (especially if you only code in AS2), you'll find that the getter / setter methods don't actually work on objects of that class.

Half a day and several strong cups of tea later, I finally remembered that the underlying Object class has a method called addProperty(), and that method is what you use to register getter / setter functions for an object in AS1. Actionscript 2.0 classes compile down to the same byte-code instructions as AS1, so your getter / setter functions actually end up as __get_PROPERTY() and __set_PROPERTY() and uses Object.addProperty() to register them against PROPERTY in your class objects.

So, because I has implemented a method called addProperty() in my own class, and because all classes are ancestors of Object, I was hiding the Object.addProperty() method, which meant that the getter /setters were never registered.

Having worked exclusively with Actionscript 2.0 for so long, it took me a good half day to figure out why this was happening. I just wanted to post this up here as a reminder so that others don't make the same mistake! It's certainly an easy mistake to make now that we have a proper syntax for getter/setter methods instead of having to register them ourselves. Certainly a compiler warning would have been nice!

* A really good brother would have given me proper private members instead of the pseudo 'protected' members that we have now, but that's another post for another day.

--Update--

Typical! Having just added syntax highlighting to this blog, I can see that the addProperty() method is now highlighted as a keyword. If I'd used an editor that did this in the first place, maybe I could have saved myself from this whole sorry debacle. Sadly there's a distinct lack of good Actionscript 2.0 editors for OS X, basically it's TextWrangler or bust!

Categories
Flash
Comments rss
Comments rss

« PHP for Flash Delegate class refined »

7 responses

You should try TextMate. I have been playing with

Will | Wednesday, 16 February 2005 | 1:21 am

You should try TextMate. I have been playing with a command that will send my AS project to mtasc for compilation, send errors as HTML to TextMate and then show the updated file. I can do all this without launching the Flash IDE.

Will: I keep giving TextMate a try every time they

Steve | Wednesday, 16 February 2005 | 11:33 am

Will: I keep giving TextMate a try every time they release a new version, and I absolutely love its project folder view and code collapsing, but I’m currently lumbered with an old 733MHz G4 (until next week when I get a shiny top of the range G5 - woohoo) and it’s seriously sluggish when trying to scroll around a large .as file.

Steve: Really, I would not imagine that it would

Will | Friday, 18 February 2005 | 12:06 am

Steve: Really, I would not imagine that it would be so sluggish. Did you email the author about this? I ran it on my G4 1.25 Powerbook, 1.67 Powerbook and G5, they all behave about the same as far as scrolling. How many lines does your file have?

Steve: Can you shoot me an email. I

Will | Friday, 18 February 2005 | 12:07 am

Steve: Can you shoot me an email. I could not find your contact info anywhere!

Will: Email Sent. I didn't email the author, as I

Steve | Friday, 18 February 2005 | 10:22 am

Will: Email Sent. I didn’t email the author, as I just assumed that my ageing G4 was past it - there’s a heck of a lot of difference in processing power (almost double, not to mention bus speed improvements in later G4s) between a G4 1.25GHz PowerBook and my G4 722MHz PowerPC. I have problems with files that have ~700+ lines of code.

hi steve, is it possible to take a look at

jeremy | Wednesday, 02 March 2005 | 1:12 am

hi steve, is it possible to take a look at your flash-based CMS ? or online-demos ?
thanks

Steve I know this is probably the wrong place, but

Tim Thompson | Saturday, 19 March 2005 | 10:49 pm

Steve I know this is probably the wrong place, but I want to learn about the Flash-Based forum that you created. and possibly get the soure so I can create one of my own, for an existing project. Please email me asap maddogg54@comcast.net

Leave a comment

You can use these tags : <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

About Dynamic Flash

Steve Webster is a Senior Web Developer for Yahoo! in London, UK.

He is more than a little concerned that he defines himself in terms of his career, and that he talks about himself in the third person.

Find out more

Recent Posts

  • FotB ‘08 Schedule: iCal feeds
  • Read and write local files with Flash Player 10
  • The problem with SproutCore
  • Yahoo! London seeks Junior Developers
  • I am Singularity

Tags

Accessibility ActionScript actionscript3 actionscript 3.0 air apple astro book calendar conference designer filereference file upload Flash flex fotb08 framework ical internet internet explorer JavaScript jobs junior microsoft opportunities ria schedule singularity08 skin tutorial urlrequest web developer web development Web Standards Yahoo!

del.icio.us-ed

Stuff

Singularity?
Flex.org - The Directory for Flex
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox