Well this is just a little article that will hopfully give some of you out there some insight and save you some time. I will be editting and adding to this as I see fit, it is not finished...this is just a small start.
********************************************************************
PLEASE REMEMBER-DO NOT POST QUESTIONS ON THIS THREAD OR THIS SECTION OF THE BOARD. POST YOUR QUESTIONS UNDER THE GENERAL OR ACTIONSCRIPT SECTIONS. IF YOU FIND ANY ERRORS, AND I KNOW THERE WILL BE SOME, PLEASE EMAIL THEM TO ME.
-THANKYOU
********************************************************************
Paths in Flash
Now we have all come to the point in time where we just couldn't get the path right for a variable, an MC (movieclip), or button. This article will hopefully help you with your problems, so needless hours are not wasted on pointless things.
Your maintimeline is the basis of your movie. It is where everything is. Now when you are coding on your maintimeline it is all good and fun. No problems, all you have to do to populate a dynamic texbox with what you want is set the variable equal to something. Say your variable for your textbox is "test" and you want "Hello" to be displayed in it. As long as the textbox is on your maintimeline, then this is all your need for actionscript....
test = "Hello";
Basic "_root." ReferencingNow this is where we are going to add something that might be new to you. The "_root." command. What is it you ask?....well it is a reference. It means "on the main timline."
Now in the last example of populating your dynamic textbox with the simple message "Hello" you didn't need the "_root." to tell it to send the value to a variable on your main timeline, it defaulted to it. Flash automatically looks for and sends to variables in the location that the code is in, with some acceptions that we will get to later (load commands). Now say that you put the _root. in front of the variable....
_root.test = "Hello";
Yes, it does the same exact thing! Not very impressive, huh? Well, it will get more interesting and helpful later. Even though this does the same exact thing and is not required, you should get used to using it. Use the _root. method rather than just putting the variable.
Now say that we decide to put that dynamictextbox in an MC (movieclip) ? If the code is on the main timeline, it no longer works. It is still looking for the variable on the maintimeline still. NOw we need to tell Flash where the variable is. To do this we will need to use the instance name of the MC that we put the dynamic texbox in. First you need to select your MC and go to the properties window and give that thing a name. Without an instance name you cannot refer to it in your code. Lets name it "dave". I chose this name to keep things from getting confusing..."dave" is just a random name I thought of. So now we can change our referencing and tell Flash where the textbox is.....
_root.dave.test = "Hello";
"_root." starts your referencing from your maintimline, "dave" specifies the instance name of the movie clip which the textbox is in, and "test" is the name of the variable for the textbox.
Now you could make things more difficult and put the dynamic textbox in an MC that is inside of another MC with the instance name of "bill" on your maintimline. It is just the same with an extra name in your dot syntax reference....
_root.bill.dave.test = "Hello";
Basic "_root." MC ControllingThis basic "_root." referencing also applies to other things like controlling MC from another MC or controlling your main movie from an MC. It is all very simple. Say that you have an MC on your maintimline with the instance name of "jenny" and it has 5 pictures on it. You have a stop action on each. Each time you press a button on your main timeline, you want it to make the MC go to the picture for that button. This is all that you have to do....
on(release){
_root.jenny.gotoAndStop(2);
}
The "on(release){}" is just the event handler that tell when the code should be executed. All of this code should be placed on your button that is located on your maintimline. "_root." again is not neccessary, but is a good habbit. It tells the referencing to start on your maintimline. "jenny" is the instance name of the movie clip which you are controlling. The gotoAndStop(2) is a basic action that tell you MC to go to the second frame. That is all that it takes. There are other methods which I will descuss in a later small acticle on MC controlling (when I can spare the time), like the telltarget method.
Basic "_parent." ReferencingWith the _root. refernce you can do some basic reverse referncing, that is referncing back down the chain of symbols. The _parent command is a great way to go one step back in the symbols for referncing. "_parent." tells Flash to start your reence in the symbol that the symbol that your code is in. Now that sounds a bit confusing, but it is quite simple, let me explain. Say you have an MC in your movie, with the instance name "stan". In this MC you have a textbox with the variable name "test" and a button. Now when this button is clicked, you want a message "Hello" to display in the dynamic textbox with the variable name "test". Under the release of the button you put the code "test = "Hello"". But nothing happens. It is looking in the symbol that the code is in, the button. You need to tell it where the variable is that you wish to populate. Now this is where the parent reference comes in....
_parent.test = "Hello";
You press the button and it works. "_parent" starts the referencing in the symbol that the button is in, instead of starting in itself, (because a button is a symbol). It tells Flash to look in the MC "stan" in this case and it finds the variable "test".
This reference (_parent), can come in quite useful when you have many MC's inside each other.
Now lets step it up a notch. Say that you have the same MC "stan" with the button in it, except this time you put the textbox in another MC, with the instance name "kim", inside of the MC "stan". Your code quits working. It is looking in "stan" for the variable "test", not inside of "kim", so all that you have to do is add "kim" to the dot syntax reference....
_parent.kim.test = "test";
Again, the "_parent" tells Flash to start the reference in the symbol that your button is in...in this case it is "stan". The "kim" tells it to look for a symbol with the instance name of "kim". Then the "test" is the variable associated with your dynamic texbox.
Baisc "_parent" MC ControllingJust the _root. reference, the _parent reference can be use to control an MC from another MC or control your maintimline from an MC located on it. Since the _parent reference tells Flash to look for variables in the parent of the symbol that it is in, you can easily apply this to executing code in the parent of the symbol that you are in. Say that we have an MC with the instance name of "jack". We placed it on our maintimeline. It has two frames with a stop() action on each. We put a button in "jack". Now we want this button to go to frame 2 in "jack" when it is pressed. We can do this as described in an earlier section with the basic _root. reference, but lets try it with the _parent reference. It may not seem logical to use it so much in this case, but I will explain a more logical scenerio later on. To do what I have described above we could use this simple actionscript under the button (right click the button instance>actions).....
on(release){
_parent.gotoAndStop(2);
}
Talk about easy. You do not need to know or even have instance names. The name "jack" was
not even required. "on(release){}" is an event handler for the button.... it tells Flash to execute the code inside of the brackets when the button is release (logically it must be clicked to be released). Now the "_parent." reference tells Flash to start the reference at the parent of the symbol that the code resides in. Since it is followed by an action the _parent reference is telling Flash to execute the code on the parent's timline rather than on the symbols. There you have it, simple MC controlling with the _parent. reference. No hassle of instance names.
Now I said that I would give you a scenerio where this method of referencing would come in handy. Say, for some odd reason, you had a chain of 8 symbols on your stage. You had an MC in an MC in an MC.....etc... You want a button on the 5 MC down the line to change the frame of the MC it is in. You can use the simple _parent refernce to do such.
One more scenerio....you have an two MC's "jane" and "fred", they are both in another MC on your main timeline. Now there is an MC in fred named "will". Inside of will there is a variable name "hello". You want that variable from "will" to be accessed by jane. You simply reference like so....
_parent.will.hello
I know that that got a little confusing with all of the different names, but I hope that you got the idea. This refernce does come in handy. I use it with my loadVariables commands and I will give an example of that in a later section for the Basic 4 load commands.