AS 2.0 hooray
- Started
- Last post
- 16 Responses
- bry
after years of dicking around in version 1 code i'm finally getting my feet wet in actionscript 2.0. woo, java all over again HOORAY cough.
couple of questions for you 2.0 gurus (not much luck with google so don't ask):
1) I have a an instance of class "A" which in turn creates an instance of a class "B". is it possible to, from _within_ class B's constructor, declare a reference to the class "A" instance which created the class "B" instance? as it is right now, i have to pass a "this" from A's class to one of B's private members so that B knows who it's creator is. but i'd rather class B figure that out by itself without any work from class A.
2) what's the best way to manage depths globally throughout the movie? i know there's a getNextHighestDepth() function but that shoves newer movies underneath existing mc's but is there one that piles new movies on _top_ of older ones? or do i have to hack together a static class that bookkeeps all mc's/layers?
- enjine0
1) passing a reference to the Class A Object upon instantiation via the constructor is the right way to do it. you don't want to hardcode that object in your class or even in the .fla code itself. are there multiple class A objects instantiating multiple B Objects?
2) zillions of ways of doing it. i don't use the getNextHighestDepth() function for the same reason, and i usually just end up making a local variable and increment it as i instantiate new mcs. if you actually want to manage your movieclip depths at runtime at any level beyond simple incrementing, yeah you'd probably want to class it out.
just my $.02
i'm enjoying AS2 nowadays too, and there are a bazillion little problems like these that you run up against.just started reading http://www.amazon.com/exec/obido…
and it's really helping me a lot. share up any progress yr making with the community, i'm sure plenty are interested.
- unfittoprint0
#1 you could use the B class as an extensor of class A [class B extends A]. then to retrieve any method or variable from its parent you should use super()
#2 I don't understand how using getNextHighestDepth( ) you're getting new mc's underneath 'old' ones...
- bry0
enjine:
1) yes, it's very likely that i will be having multiple class A objects instantiating multiple instances of class B.. i'm trying to establish a tree structure between these objects -- they're custom UI widgets/components, some of which 'contain' others, which is why it's important for me to establish the hierarchy between various instances. in order to have the objects communicate w/ each other, i need to be able to propagate commands up and down the tree.2) i don't think i'll be messing around with depths much. depth management is local to each movie clip, right? i think that's what i was unsure about before..
- bry0
unfit:
1) i considered extending, but the two classes are not very related... could tacking on too much super() members/methods potentially be wasteful of resources/CPU?2) as far as i understand it, attachMovie("asdf","asdf", getNextHighestDepth()) puts the asdf movieclip underneath any others you've put on the stage via actionscript. at first i was like "huh?" but when reading the english in the function ("get the next highest depth" == "get the one just 'under' the highest depth you use") it made more sense to me... though it puzzles me why they have a function for this since i would imagine people don't slide mc's _under_ other mc's very often.
- unfittoprint0
#1 well, I recently did this menuXML class and I had to use some of the methods from a Slide class, so on the first line of menuXML class [before class menuXML{...] i wrote
//Slide being in the same directory
import Slide;#2 (getNextHighestDepth( )+2) ?
- bry0
so anyway. is the following the way i ought to get a class A reference into a class B instance?
(in class A)
var classBclip:Movieclip;public function ClassA() {
this.classBclip = this.attachMovie( "ClassB", "mc_classb", depth++, {classparent: this} );
}-----
what i want to know is whether not i can have class B's instance, on its own, figure out _who_ constructed it, without the "hint" from class A (which in this case is the {classparent: this}).
- enobrev0
i've always wanted to write some kind of advanced depth management class (Started one a couple years ago), that might even allow for managing multiple depth groups (as well as freeing depths whien clips are removed, etc). Probably a static class.
But every time i start, I get bored as hell and end up using a globel 'depth' var and incrementing it everywhere.
As far as design patterns, be sure to check out martin fowler.
As for referencing a parent instance, it's probably best to pass 'this' to the constructor.
- enobrev0
wont _parent reference classa A's clip?
- Dancer0
You guys are way beeyond me but tell me one thing:
1) I am struggling to get to grips with AS1.0. Should I carry on learning this before I try AS2.0. Or should I sod it and jump stright to 2.
p.s sorry for the Hi-jack!
- jkosoy0
I'd like to hear about your depth management ideas enobrev. I've been having depth issues lately...no problems specifically, just getting annoyed that I can keep my code clean and still feel like something is out of my control, y'know what I'm sayin?
I too am just getting started with the transition to AS2. I'm sure you'll be hearing more from me as I get confused. :)
- enobrev0
get into as2. It makes a lot more sense as far as being an actual language is concerned. A lot more powerful as well. I've NEVER been a fan of as1.
- unfittoprint0
in most classes I add a obj:Object parameter to the class constructor to target the instance. that way I can pass the obj var as a reference to any method/var/class...
Moock's AS2.0 has also a nice chapter about design patterns even tough it doesn't cover everything ...
That Martin Fowler link seemed interesting, any more regarding the subject of design patterns?
- bry0
OH DUH I'M SUCH A RETARD
you're absolutely correct, enobrev... i asked this huge question for nothing. thanks, you can all return to your regularly scheduled business.
i was confused because "this" and "_parent" mean something different now in AS 2.0 as opposed to pre-2.0 when i only used 'this' and '_parent' to refer to only movieclips, not class instances.
- JamesEngage0
i just use a value for holding Z levels... if you have loads of shapes floating in space... I found the best way to order them is convert their position to an integer and plonk the movieclip on that level.
- bry0
and there you go, Dancer.. you've just witnessed a case of "actionscript 1.0 brain".
so jump to AS 2.0 now... the sooner you do it, the less likely you'll be confused due to basing your troubleshooting on rickety AS 1.0 constructs.
- Dancer0
*shivers