PATRICK JOSEPH BESONG: July 2008 Archives
I know it's been a long time since I last posted. I've been busy and haven't felt much like writing. I decided to bite the bullet and only code in Actionscript 3.0 from now on. I've been using Actionscript 3.0 for a few months now and have a few projects under my belt. I'm slowly coming around to seeing the benefits, but it's not been easy. I'm proposing a sort of "survival guide" of code that will help people get through the initial shock of trying to get something done in a new coding environment. Here are a few tips on coding Buttons, Controlling MovieClips, and doing Navigation. So, here goes.
Buttons
You probably have heard this before, but buttons are no longer coded to do their stuff within the button itself. All code must be in the timeline. Here is the typical way to make a button function now. After giving the button a name (myButton in this case), use this sample code:
myButton.addEventListener(MouseEvent.MOUSE_DOWN, doIt);
function doIt(evt) {
trace("I love Actionscript 3.0");
}
What is kind of stupid is that even though I'm not passing anything to the function and should have no need to put anything in the parens after "doIt", it will throw an argument mismatch error if I don't put something in there ("expecting 0 got 1"). So if that happens, just type any old word in there and it'll run. Code purists will flame me, but it works. I think it's actually passing a mouse event, so a lot of people will put (evt:Event) there, but it's really not necessary for it to work.
I also see people importing classes unnecessarily like this:
import flash.events MouseEvent
before they write the button code. The button will work just fine without importing that! Too many people seem to be writing and importing packages to do code that will run without it. There is a time when you want to do that, when you are working on large projects or in groups where you want to control code more, but for most projects it is entirely unnecessary.
Also, it is important to note that if a button is not going to be in the timeline until frame 10, don't put the event listener in for it until frame 10. Otherwise it will throw and error if you have the listener in frame 1 and no button for it to refer to.
Personally, I don't know why you would need to even write an event listener for a button. This would be an improvement to actionscript if it were understood that a button by its very nature should already have a built-in event listener. Hey, whatever. Suck it up I guess. Let's move on.
Controlling Movieclips
If I wanted to use myButton to trigger another movieclip (called movie2) also on the main timeline to play, I would write the function like this:
function doIt(evt) {
movie2.play();
}
But, if I have a movieclip (movie2) on stage and it contains a button that triggers the main timeline to play. I would have to write the event listener and function in the actions within that movieclip. Here is how I write the function (using the same button as before):
function doIt(evt) {
MovieClip(this.parent).play();
}
So, from within that movieclip where the button is, I have to come up a level before I can tell the main timeline to play. You have to tell it that the main timeline (this.parent) is a movieclip or the play function will not run, hence the MovieClip() reference.
So, to get a little more tricky, let's say I want that button within a movieclip (movie2) to control yet another movieclip (movie3) on the stage. Here is how I'd write the function:
function doIt(evt) {
MovieClip(this.parent).movie3.stop();
}
I think you get the idea.
Navigation
Let's say I have a series of photos that I want to create navigation for. To make Previous and Next buttons work, after creating and naming the buttons, I wrote the following code in the first frame, which I named "photo1":
var nextLabel:String = "photo2";
next.addEventListener(MouseEvent.MOUSE_DOWN,goNext);
function goNext(evt) {
gotoAndPlay(nextLabel);
}
stop();
Note there is no Previous button code since I'm only on the first photo. On the first frame of the next photo, I will name the frame "photo2" and in the actions I will have:
nextLabel = "photo3";
var prevLabel:String = "photo1";
prev.addEventListener(MouseEvent.MOUSE_DOWN,goPrev);
function goPrev(evt:String) {
gotoAndPlay(prevLabel);
}
So, in the first line, I'm updating the nextLabel variable to be "photo3" and introducing a new variable called "prevLabel" that points to "photo1". I then add an event listener for my Previous button which is now on the stage along with a new function called "goPrev" that takes me back to the previous frame label. Now for photo 3, all I need to do is to update the frame labels in the actions, which would look like this:
prevLabel = "photo2";
nextLabel = "photo4";
There may be a sexier way to do it, but this works without a lot of hassle dealing with label arrays and such.
Okay, that's it for now. Hopefully this will help you through a few sticking points so you can get on with your projects in Actionscript 3.0.
