views:

19

answers:

1

i would like to make my code easier to read by replacing long paths with variables, or something similar.

in my code, i can target an instance many times, but the instance can have a lengthy path deep within the display list. for example: myButton instance could be located at myButtonsPanel.section2.redArea.myButton, or something like that.

is it possible to substitue this long path as a variable or constant? something like:

var myPath = myButtonsPanel.section2.redArea;

therefore, calling the instance would be:

myPath.myButton;
+1  A: 

Not the path , but the actual Button

var myButton:MovieClip = myButtonsPanel.section2.redArea.myButton;

although you could also do this:

var myContainer:MovieClip = myButtonsPanel.section2.redArea;

//then access your button like this
myContainer.myButton

It really depends on what you need to do, but the idea is that instead of storing a path, you're actually referencing a MovieClip directly, then using that variable to access this MovieClip

PatrickS
thanks, this works fine. however, i'm a little surprised that only MovieClips seem to work with the container approach and not Sprites. i never use MovieClips since i don't use the timeline, and export my IDE assets as Sprites in the properties panel. is this expected? why?
TheDarkInI1978
It should work with any display object.
TandemAdam
@TDI1978. Probably you're doing something like: `var myContainer:Sprite = myButtonsPanel.section2.redArea;` and then `myContainer.myButton`. `Sprite` is "sealed". Meaning, it only has a fixed set of properties / methods. There's no `myButton` property in `Sprite`. So the compiler will give you an error. This compiles if you type `myContainer` as a `MovieClip`, since a `MovieClip` is `dynamic. So, properties could be added to it at runtime. So, by definition, it *could* have a myButton property at runtime and hence the compiler will not complain (though your code could just break at runtime).
Juan Pablo Califano
@TDI1978. However, if you type `myContainer` to some custom `Sprite` with a `myButton` property (I mean, you create a class that extends `Sprite`), provided there's no other typing problem somewhere else it should compile fine.
Juan Pablo Califano