PDA

View Full Version : Trouble with new conventions



Blips
August 20th, 2007, 08:57 PM
Hey guys, I'm pretty experienced with as2. I've been doing OOP in it for a long while now.. but they changes from as2 to as3 have completely thrown me off. Things that were once the simplest things now are impossible for me to do. I need help with a few things :

MovieClip(root).foo
I keep reading how this casts root as a movie clip, but unless the syntax is extremely unique, this looks nothing like a cast in Java or C++. Can someone give me a detailed explanation of why this is needed and what it is achieving?

Playing movie clips with code
I'm attempting to do something very simple, play a movie clip when a variable has attained a certain value. Lets say the movie clip is I'm trying to play is "bob". The path to the this movie clip is root->environment->bob. When I attempted to do this I would get the error
ReferenceError: Error #1069: Property environment not found on flash.display.Stage and there is no default value.

Garbage Collector
I've read it's only possible to delete dynamically created object properties, and it's impossible to delete anything else that is created dynamically. The only other option is to set that objects value to null. So if an object has been set to null, is it ok to assume that the garbage collector will free up that memory relatively soon?

Looping
In the past for my games, I would simply create a function in the main timeline that would loop forever once the main timeline reached that frame until I stopped it. How could I achieve something similar in as3?


Thanks for any replies and help.

Krilnon
August 20th, 2007, 09:24 PM
MovieClip(root).foo
http://www.kirupa.com/forum/showthread.php?p=1882881#post1882881


Can someone give me a detailed explanation of why this is needed and what it is achieving?

It's needed because root is only know to be a DisplayObject; it could be a MovieClip, Sprite, or something else depending the application. Hopefully you are already familiar with why you need to cast in general, if you are familiar with C++ and Java.


The path to the this movie clip is root->environment->bob. When I attempted to do this I would get the error

You should be using DisplayObject.getChildByName if you are trying to access children of DisplayObjects by their names. Even though MovieClips are dynamic, properties named after a MovieClip's children aren't created when you add the children. (So, a MovieClip child_mc can't be accessed by accessing its parent's child_mc property unless define that relationship yourself)


So if an object has been set to null, is it ok to assume that the garbage collector will free up that memory relatively soon?

Basically, if all references to an object are null, then the garbage collector is supposed to collect it eventually. There aren't any Adobe-approved methods to force the garbage collector to run. Of course, there are a few large threads discussing this already, so I will just link to one of them and let you look for yourself: http://www.kirupa.com/forum/showthread.php?t=262507

Someone else will probably step in, yell at me, and then point you to some better resources or answer your collection frequency question directly. If not, check out the link and search the forums!


In the past for my games, I would simply create a function in the main timeline that would loop forever once the main timeline reached that frame until I stopped it.

What do you mean by that? I'm going to assume that you are not talking about a for or while loop, but a frequently occurring event like ENTER_FRAME. If so, then you can continue the same practice, though you will have to use the new event structure instead of the old one, of course.

Blips
August 20th, 2007, 09:30 PM
Wow thanks Krilnon, you've been very helpful. Thanks for answering all 4 of my questions. Sorry about that last one, you were correct in assuming I meant a event that acted like the ENTER_FRAME.

devonair
August 21st, 2007, 05:56 AM
Regarding:

MovieClip(root).foo

Keep in mind that it is never necessary. It's basically a "hackish" workaround to allow Flash users who developed bad habits in AS1/2, to continue using those habits in AS3 and breaks some pretty basic OOP principles. Macromedia, then Adobe, have been recommending not referencing the _root property since at least Flash 6 (AS1). This allows you to keep referencing the root object (whether it be MovieClip or Sprite) in Flash 9 (AS3).