Haven't written anything on here in a few days, been busy learning more AS3 and getting on with my 2 current game projects. I've got a list of things todo, plus some things i've found out about actionscript as i'm learning which might be helpful to others.
My background is from all sorts of programming, i think a rough list is BASIC, PASCAL, AMOS, BLITZBASIC, AREXX, ASM (680x0), C, C++, ASM (80x86), LUA and now Actionscript 3. Having spent a lot of time with C++ and Delphi (oop pascal) i've got quite used to doing things in a certain way. Actionscript 3 in Flash 9 is pretty good so far, certainly i could never have created anything useful in flash previously - i think i have two left thumbs ;). I can just about "operate" paintshop pro/photoshop, but haven't really produce anything i'd be in a hurry to show! So flash and it's designer friendly interface wasn't for me. Since choosing AS3 as my new language to work on, it's been a slow start - but getting better, theres a definite way to do things with is a bit different.
One potential problem i've come across is with the lack of destructors for classes. Each class has a constructor (the function with the same name as the class) which you use to create an instance of a particular class like the following line of code
var myInstance:MyClass = new MyClass();
So when you create the instance, it calls that function and executes the code in it, so you can perform actions and setup data on it's creation. Exactly the same as most other OOP languages. Now normally their would be a matching Destructor, which gets called when you "delete" the instance. Flash 9 / Actionscript 3 doesn't have this particular feature. Now for the majority of the time, it's not an issue. But i've seen a problem with timers. If you create a timer in a class, and if that timer is still running when you throw away the object, the object doesn't get garbage collected and just hangs around in memory sucking up resources (so if that happens a lot, that could be a problem building up). I've looked around and theres all sorts of solutions, some very compilcated, others wrapping everything with weak referenced dictionaries and some very simple but not fool proof.
Ok, i need a quick fix to use with this. Everywhere i throw away one of my instances with something like
myInstance = null;
I'll use the following instead
myInstance.CleanUp();
myInstance = null;
And in every class i create i'll add a function called CleanUp() and put all the code to make sure i tidy everything up that might cause problems in their. And hopefully that's nice and simple, and straightforward and it won't clash or cause a problem in the future, till Actionscript gets proper destructors! Nothing amazing, but if you keep it simple and stick to what you think is right then thats a good lesson right there. If i get into this habit, then i shouldn't end up making too many memory leaks and other problems of lost eventlistners and timers and other possible problems!
I'm going to start posting hints & tips that i find as i stumble my way through AS3. If you've got any to share - leave me a comment!
Post new comment