There are a couple of things off in the code you wrote. How you target objects depends on where you place your code. Assuming you have assigned the instance names as indicated (which are not the same as library names), you would use the following in AS2 to target things to happen if you place the code in the main timeline (I recommend using onRelease)...
mc_buttonMenu.btn_home.onRelease = function()
mc_buttonMenu.btn_contactUs.onRelease = function()
So, I don't need to do anything with _root?
Also, I can see the structure now of:
movieClip.buttonInstanceName.functionYouWantToUse = function()
1 person found this helpful
Got two things for you. First I wouldn't it like this:
In your specific case it was a moveclip instance with a button instance inside it. But it could be a movie inside a movie or a FLVPlayback component inside a movie or...well you get the idea.
Also in your case you are actually trying to get to an event (onPress, onRelease, etc.) But you might want to change the _x value or get some other thing.
That all is just a subtle point of talking about it. Hopefully it helps make it more clear as opposed to less!
Another little issue.
The way you have listed there is called "anonymous function" because the function you are defining doesn't have a name. There is nothing wrong with it per se and I think most folks start out that way. But it is better to used named function. The way you have here means that if later you need to use that function again you will have to type it out again (or copy and paste). And then if you make a change to how it works you need to go and find all the places it is typed out and "fix" them.
So a better way to do this might be:
mc_buttonMenu.btn_home.onRelease = handleHome;
function handleHome function()
Notice that in your assignment there are no parens after the handleHome. The parens say, in effect, execute this function now. In this case you don't want handleHome to happen when you assign the event handler, but rather when the event actually happens, that is when the button is released.
Thanks. Where would I put the named function? Would I place this on a layer on the main timeline and call that layer 'functions'. Then, I could just call that function by name everytime I wanted to use it.
I know c++, so I understand about classes if this is the direction you are headed. :-)
1 person found this helpful
In general, you should avoid using _root references unnecessarily. Some people use them as if everything needs them. Most designs can avoid them entirely... though it is an occasionally convenient way to target something that can make anyone yield to the temptation..
Ned, I got it to work!. I placed that code on the main timeline. It did not work at first. So, I looked at all of the instance names and everything was not named. Once I supplied instance names for everything, that code worked.
I made the same type of thing for contactUs and that worked also.
I am pumped. Thanks man!
I tried this method also. I placed the code on the main timeline. However, I got some errors. Am I supposed to put the function definition in the same location as the code that calls it?
What errors are you seeing?
Yes it should be in the same scope as the code the calls it. So if your event handler assignments are on the main timeline then having the code on the main timeline is the place it aught to be. However if your assignment is inside a movieclip or something then you might want to move the assignments to the main timeline.
While it is possible in AS2 to put code on timelines, within timelines, within timelines, and also directly onto clips, it isn't a great idea. It gets really difficult to find the code when it is spread out through all locations. It is generally better to have all the code on the main timeline so that it is easy to find.
It isn't such a big deal if you just have a few lines and a few things going on. But it helps you structure your code better and leads to a different way of thinking about your code.
It isn't strictly related to classes and oop, but it is a step in the direction of encapsulating and reuse.
If you have experience with C++, and you are just starting off learning Flash, I encourage you to skip AS1/AS2 and start by learning AS3. For some people AS3 can be a bit daunting to begin with, but the more experience you have with classes and OOP in general, the more likely you are to fair pretty well in picking things up easier with AS3.
Another benefit of AS3 is the error reporting... AS2 can leave you staring at a design sitting at a standstill wondering why Flash doesn't work (it works quite welll BTW), but AS3 is very generous with error messages and can be undaunting when it comes to making you follow the rules.
Yeah I agree with Ned. If you don't have an overwhelming need to learn AS1/2 then starting with AS3 is a much better proposition. Even without the C++ experience you would probably be better served to go the AS3 route.
At my work we have to publish to Flash 7 so we can't use it, but most places are going to be looking more toward the future...
I was going to make a new posting, but this kinda falls under this title anyways.
I added a preloader to this movie. I had the movie working correctly and saved that version before I added this preloaded. Pretty much, I pushed everything from the first frame to the second frame on the main timeline. Then, I added a new layer called preloader. I created two movie clips. There is some action script also. I am pretty sure all of that is working correctly.
I had some flicker, but I fixed the errors with that. The main movie does not flicker now. However, I cannot get my buttons to go to frame "11" or to the label "contactUs". I am pretty sure that the frame "11" is because I pushed the main timeline to frame number 2. Does this have something to do with how all of the frames are totalled? Therefore, by putting frame "11" in the action script, that would no longer work?
So, if all of that is true, how do I tell a button to move to a label?
I thought this was what I put:
That does not seem to be working for me.
However, maybe I have a separate issue.
There is one error that is shown. This error comes up after I Test Movie. I do not have the file in front of me. I can post that error later. But, why would adding a preloader cause my buttons to not work?