8/10/2007
What makes ActionScript 3.0 so good for Flash game development?
Download the audio file.
Transcript:
G: Hi and welcome to the Flash Game University podcast, http://flashgameu.com. I'm Gary Rosenzweig and with me is William Follett.
W: Hello.
G: Hi and in this episode I wanted to take the time to talk about ActionScript 3 which is really what it's all about. The book ActionScript 3.0 Game Programming University, as the title suggests, is about ActionScript 3. The site, even though there's no problem going to the forums and talking about earlier versions of ActionScript, it's really about game programming and ActionScript 3.0 means game programming, it really does.
W: That's right.
G: That's one of things that makes me so excited about it, it's ten to a hundred to even a thousand times faster than earlier versions of ActionScript. Which is important. I mean you think in a game, an example, you want to have a space rocks type of game, you have things floating around, you can have sixteen different rocks floating around and your ship and you're firing bullets, a lot of collision detection. You've got ten little laser blasts floating around in space, you've got sixteen rocks, you've got you're ship. You have to detect everything is colliding with everything else. It's a big loop, big test, big mathematical test being done and earlier versions of ActionScript that would take awhile. In ActionScript 3 it just happens, so fast.
W: Okay, alright, a pool game.
G: Well, ah, a pool game.
W: Would that be faster?
G: Yes. Well and actually...
W: 'Cause your ball hits the other balls...
G: Oh my.
W: And each ball has to compute.
G: It goes exponential, especially when you're breaking. So, there's things like that, that are just because of the brute force in ActionScript 3. The way it does it, is there's a new engine, there's actually two engines in modern Flash. You download Flash 9 player. There's one engine that interprets earlier ActionScript, ActionScript 1 and 2, and an engine that interprets ActionScript 3. The engine that interprets ActionScript 3 is way faster than the other one. If you stick to ActionScript 3 you can do all these calculations way faster. One of the ways it's faster is there's something in ActionScript 3 called strict typing.
W: Strict typing.
G: Strict typing, yes. What this means is you create a variable, and you say what that variable is going to hold. In earlier versions of ActionScript, even though ActionScript 2 allowed you to do typing, it wasn't strict typing. You could basically have a variable say, variable A. Variable A could be a string, a bunch of characters, it could be an array, it could be a number, it could be all sorts of stuff. In ActionScript 3 you might say it's an integer and that's all it's ever going to be.
W: So A will always be an integer?
G: Yeah and the speed is incredible. So, for instance, the three type of numbers are number, which is the equivalent to like a floating point value, you know something like 7.839 or whatever.
W: Yeah.
G: There's integer, which is you know a number like 1 or -5, and then there's even one called unsign integer, uint, which is just zero and up.
W: Okay.
G: The difference between number, handling a number, a number has got to be this thing, it's got to hold like decimal places, it's this complex construct and if you're going to do something like loop a million times and you're going to use this complex construct to represent this number you're looping, you know, 1, 2, 3, 4, 5, it's a lot more memory access, a lot more calculation each time to compare that number and see if, 'Is it equal to three?' Well, I don't know, I have to use this weird construct to determine if it's equal. But if you say it's an integer, it's a very simple little construct, a tiny little thing represented almost by bare bits in the computer. You can compare it, you can change it very easily. So, you can do like a loop using numbers, and then you can do the loop using integers, it would be like a thousand times faster, in my test.
W: Oh.
G: And the same thing with a string. Say, hey this is nothing...never going to be anything but a string. It's a bunch of characters, it's very simple, as opposed to having this overall, it's going to this object, it could be anything. That makes it really easy to do things and fast. So that's one of the things ActionScript does. Another it does, ActionScript 3 does, is it changes how things are displayed. If you've done a lot of work in ActionScript 1 or 2 and you add movie clips to the stage using ActionScript, you know that you've got this weird thing where you've got to add it to the stage, and then there's like depths and you can swap two things, be on top of each other, and it's really strange and weird how it works. ActionScript 3 throws that all away and everything is just, everything is children of the stage, you have the stage and you have it's children. So it's children may be three movie clips. Movie clips, they may actually have children inside them. So it might be four movie clips inside this one movie clip. And then each thing it just has a list, this is first name I'm going to draw, this is the last thing I'm going to draw.
W: Okay.
G: And you can insert something in the middle of that list, in the front of that list, in the end of that list and it's done. There's no strange swapping and having something that's at like this layer and that layer. It makes it very easy to things and of course it makes it fast. Another thing ActionScript 3 does to make things fast is it has something called a sprite. And in old versions of ActionScript we had something called a movie clip. Movie clips are basically, like the Flash movie is one big movie clip.
W: Yeah.
G: And then you can have movie clips inside that that are mini Flash movies that have their own timeline, they've got their own elements, and they can have movie clips inside of those, it's encapsulation, you can go as deep as you want. Well, what really slows that down for a lot of stuff is you put something on the screen, like say a little space ship. And the space ship doesn't have animation, it doesn't have anything, it's just an image. Inside that movie clip is an entire timeline structure for how that might be animated, how it might have labels and frames and key frames. Well, a sprite says it's going to be like a movie clip, but it's not going to have any of that. There's not going to be any timeline. It's going to be a static element, that's just going to be this one thing and it makes it so much easier. But without a timeline, it can render this thing on the screen very quickly. Now a sprite can actually have more sprites in it, and a sprite can actually have movie clips in it, that's fine, but the spite itself is not something that needs to be, you know, animated or whatever. So it's simple. It allows you to go and choose and say this is a simple object, this is a complex, animated object.
W: That makes sense. If you're going to rotate a space ship on the screen you don't need the animation frames stuff. It's a waste.
G: Right. You may have a dozen movie clips, but you may also have a hundred other elements that are not animated in anyway.
W: Okay.
G: These are all called display objects and then there's other types of display objects, like a little text field, for instance, is a display object. You know video is a display object, that type of thing.
W: Okay.
G: So, it breaks it down into more basic elements that are easier to understand, easier to build with, and easier to tell Flash exactly what you want and no more than what you want.
W: Yeah.
G: So, that's the three main reasons why Flash ActionScript 3 is faster. It is going to be tougher for people that have been doing ActionScript 2 and before to get used to.
W: Now do they have to relearn a lot of stuff?
G: There's some stuff, you know there's some stuff that's exactly the same, doing little conditional statements, if statements, for loops, that type of thing. There's some stuff that you have to get used to. You have to declare variables, for instance, a four loop, you would have said 4(i=0; etc. Now you have to say 4(Var i, and then you have say, 'What is i?' :int, for an integer =0. And then you can't, if you use i again in the same one, you can't say Var again, because you're not re-declaring it. So, some things you have to get used to. One of the big things, for instance, creating a button. Simple thing of creating a button. In ActionScript 2 and earlier, you can actually put a button on the screen and have a script attached to it, you know the script for that button, that says 'On press, and then like Go To and Play this frame.' You can't do that in ActionScript 3. You can't attach scripts to buttons actually.
W: How am I going to use buttons if I can't do that?
G: You assign a name to the button. You say this is 'My Button.'
W: Okay.
G: And then in the timeline you would actually say, okay, 'My Button. add event listener.'
W: You would 'add event listener?'
G: So 'add event listener' would say okay, I'm going to add an event listener, something that listens for events on this button and then you assign it, the event I want it to listen to which would be a mouse event, 'click' and then you would say when mouse event 'click' happens call this function. So the function might be 'My Button function,' and then that tells that button now to listen for a mouse click. Then you create a function called 'My Button function,' and you put inside that function, 'Go To and Play this frame.' So that's a good example of one thing was simple that's now very complex. But at the same time, once you get programming there are things that were very complex before, manipulating lots of objects and stuff, that are so simple. You know you have to get used to it. It'll take some getting used to. But I find things that took me awhile to do that I looked at 'Boy it's going to be several days before I get this game up and running in any state,' even a very basic state, I can get a game up and running in like a day. And it's just really quick and easy to do that and it's all fast. Usually have to learn to think a little different. You have to learn to do things differently. You can't Add Movie anymore. You know you actually go ahead and you assign an object identifier to a symbol in the library and then you actually create a new version of that. So say you had something called 'My Movie Clip.' In the past you would say, like Add Movie, or Attach Movie sorry, Attach Movie - My Movie Clip.
W: Yeah.
G: It would go onto the screen and then you'd have to manipulate where it was and all this stuff. In ActionScript 3 you would actually go and call it, in the symbol, you would actually attach an identifier to it, a class, that was called My Movie Clip and then in ActionScript 3 you would say, okay let's create a new My Movie Clip, just like you were creating a new variable, like a new integer, or a new string. You would be creating a new version of this movie clip and then you would say Add Child, and add the child to the stage or to another movie clip. That's how you would do that there. The interesting thing is that you can actually create an ActionScript class, a little AS file that's for My Movie Clip. So every time you create a new version of it, it's its own code, self contained.
W: Okay.
G: It makes it very powerful and very object oriented as well.
W: And very fast.
G: Yeah, and great as a game development platform. The encapsulation is great. Being able to do things like, oh for instance, I was just programming a game this weekend and I had, just to get things done really quickly I had everything just going to the screen, on to the Stage. So I added a bunch of objects, and I added a bunch of enemies and I added a bunch of things that are flying through the air in the game and I put them on the stage and then I realized, 'I really want to encapsulate that,' inside of something. Because I want to have some elements that appear on top, some background elements that appear on the bottom. So, I just simply, everywhere that it said Add Child, I actually said, instead of adding child to the stage I added child to a movie clip that I created. And every place I put Remove Child, so I just modified those commands and I knew that was all I needed to do.
W: It was that easy.
G: And then everything was in this movie clip. It was encapsulated in this movie clip and I was able to put that in a layer under the foreground, above the background. And it was very easy to move those things around and it's kind of neat. Quick, rapid development using techniques like that. So, very good.
W: Rapid development that pays off, too.
G: Rapid development that pays off, yes.
W: I'm so blown away when you're showing me the sidescroller with the animated background.
G: Yes.
W: And how fast it was running.
G: Yes, oh yeah.
W: I've never seen that in a Flash game.
G: I'm really so psyched about how fast it is and some of the cool things. We've got three or four now, ActionScript 3 games, including the one I'm working on right now that'll be up on the site and then they'll be probably taking over the site in ActionScript 3 really as pretty much most of our new development is going to be ActionScript 3 games that'll be up there. So anyway, I'm excited about it and of course you can read in the book, ActionScript 3.0 Game Programming University, at the end of the month, more about all the cool ways to program games and there'll be more stuff at the site as well. If you've got a question about ActionScript 3, especially as it pertains to game development, ask it, at the site, by clicking on the Ask Gary link and I'll answer it in the blog.
W: Or you can mention it in the forums as well.
G: Yes, go to the forums as well. Let's get the conversation started. So, great. That's it for our initial set of opening podcasts. We'll be doing more of these and also some video podcasts with some tutorials, showing you on-screen how I do things in the future. Thanks a lot. Bye.
W: Bye.