Actionscript 3.0 - First Impression

| | Comments (0)

Honestly, my first impression was - WTF? Actionscript 3.0 is quite a departure from 2.0. There are quite a few differences that will change the way you develop in Flash. I thought a good way to learn 3.0 was to take the simulation I did and recode it to meet 3.0 specs. I figured if I was going to give this away in San Diego, I might as well try to be current and give it a little longer shelf life. This was no easy task as I was to find out.

One big change in 3.0 is that there is no code permitted in buttons. All the code for controlling buttons is written into the actions layer of the timeline. Although it's quite a different way of working and requires more code, the one good thing I can say about this rule is that you don't have to keep opening buttons to see what the code is and where to change it. It's all there in the timeline. The bad part also is that ... it's all there in the timeline. At least opening the button you know what code goes with the button.

Another thing is that buttons don't automatically understand that they need to do something when they're clicked on. You have to tell Flash to listen specifically for when a button is clicked. In AS 2.0, I could click open the Actions panel for a Submit button and type in:

on (release) { play(); }

In AS 3.0 I have to open the Actions panel for a frame in the timeline and first create an event listener object like this:

submitBtn_mc.addEventListener(MouseEvent.CLICK, submitAnswer);

Then I need to write a function that will be carried out when it's clicked on:

function submitAnswer(event:MouseEvent):void
{
play();
}

In my simulation, however, it wasn't as simple as making the timeline play. I needed it to check which radio button was selected, update the counter, and then play so it would have to reload the data in the Flash movie. So here's what my Submit button does when you click on it:

submitBtn_mc.addEventListener(MouseEvent.CLICK, submitAnswer);

function submitAnswer(event:MouseEvent):void
{
photo.removeChildAt(0);

if (button1.selected == true) {
counter = destination1;
play();
} else if (button2.selected == true) {
counter = destination2;
play();
} else if (button1.selected == false && button2.visible == false) {
counter = destination1;
play();
}
}

Note the phrase in the above code that says:

photo.removeChildAt(0);

I had to add this because if I didn't, when I loaded a JPEG image into Flash, it would always stay there and not get replaced when a new one is put there to replace it. The new one would just go on top of it on another level. The code says to remove the child of "photo" (the name of my movieclip that I add the jpeg to) that resides at level 0. This would not have been necessary in AS 2.0, however, it looks like there may be more options if you can load more than one image into a movieclip on different levels.

Another whacky thing that really gave me a WTF Moment was that I could not easily change the font size and color of the text next to my radio buttons. The components in AS 3.0 are different than AS 2.0. I can no longer just change a few parameters for my component in the Component Inspector. To change the size, font, and color, I have to write some special code like this:

var tf:TextFormat = new TextFormat();
tf.color = 0xFFFFFF;
tf.font = "Arial";
tf.size = 14;
button1.setStyle("textFormat",tf);
button2.setStyle("textFormat",button1.getStyle("textFormat"));

That's real intuitive, is it not?

And here is the code I had to use to load the jpeg into a movieclip:

var request:URLRequest = new URLRequest(imageArray[counter]);
var loader:Loader = new Loader();
loader.load(request);
photo.addChild(loader);

No more simple loadMovie() commands.

One thing I do like in AS 3.0, however, is that to make a button invisible, you no longer need the underscore in front of "visible". It's just:

submitBtn.visible = false;

I did manage to get the whole simulation working in AS 3.0, but it took me a lot longer than I had ever anticipated. It also took a few bulletin board posts and some nice people to help me figure out WTF I was doing.

Leave a comment

About this Entry

This page contains a single entry by PATRICK JOSEPH BESONG published on December 14, 2007 10:24 PM.

"Centre County Marching Band Festival" - Flash site created by my son was the previous entry in this blog.

SlingBox - this is pretty neat is the next entry in this blog.

Find recent content on the main index or look in the archives to find all content.

Powered by Movable Type 4.01