Join Joseph Labrecque for an in-depth discussion in this video AS3 language fundamentals, part of Getting Started with ActionScript.
In this lesson we'll examine some basic AS3 syntax and structural object oriented elements through the examination of the ActionsScript class file. So, here we can see a basic class file that's been opened up inside a Flash Builder and the way that this file is organzied, is it begins with a package declaration. All class files in ActionScript 3 will begin this way. This file, self, simply begins with package. But you can also tie the package to a specific package, where this is more generic here. So if I wanted to tie this to a specific, let's say, sound package, what I would do is invoke a reverse domain syntax package name on here. So, if I was going from josephlabreck.com, I would say com.josephlabreck.sound and that would be my package. This would have to match the folder structure that this particular class exists in though and you can see here that since everything. All of these different classes simply exist within our default package, it's fine to just declare package here without any sort of identifier. So, the next thing that we have are a number of different imports. So we're importing all sorts of different classes, from different packages, in the core Flash Player set of classes.
So here we're importing the, the Sprite class from the flash.display package. Here we're importing the Event class from the flash.events package. And you can see that we use the keyword Import to actually make that happen. We need to Import any classes that we're going to be using within a specific application. Scrolling down, we declare our class itself. So the class here is called AS3_sound. And you can see the filing is also AS3_sound.
The class name, and the file name must match precisely. And this includes case. So if it has a capital AS3, as it does in this case, you have to make sure that it's a capital AS3 over here as well, in the filename. Within the class, we can begin to declare various members, or properties of the class. So, we can see here that we have a private constant named sound file, which is typecast to a string. So, in ActionScript 3, you have this concept of strong data typing. Every function and every property has to be data typed to a certain class. So, you can see here that this constant sound_file is typed as a string. And we assign a string to that, with the, the equals sign. That's the assignment operator in ActionScript.
For any of these variables that we see here, we're simply declaring them, so this, this variable sound is a sound object. This S Transform is a SoundTransform object. this id3 variable is a boolean object, so on and so forth. It's best practice to actually do this to every single member of your class. And the reason for that is because, when you compile your application, Flash Builder, or Flash Professional, whatever you're using, is going to be able to inform you that things may be mismatched. So if you're trying to input a string to a boolean it'll let you know, hey, this data type is wrong.
So, a few differences here, we have certain of these members being defined as constants, and certain members being defined as variables. The main difference here is that a variable can actually change it's value overtime through the life of the application whereas a constant is set immediately and can not ever change. You'll also notice here that we have a variety of different access modifiers. So here, you can see that all of these are cast as private. But we also have, for instance, this constructor function here that's being cast as public.
So you'll see a number of these as you go on your way to discovering ActionScript. And there are generally four of them. There's Public, Private, Internal and Protected. When you see Public, Public indicates that this particular method or property is going to be able to be accessed from any other class, no matter where it lives. If you see something that's Private, that's the exact opposite. A Private variable or a Private method can only be accessed from within this particular class. When you see something that's cast to Internal, an Internal access modifier only allows classes sharing the same package to access that member. And when you see something cast to Protected.
Protected will only allow sub classes to access that. So what does subclass? A sub class in this case is this particular class right here. So we see that we are extending Sprite, so AS3 sound is a subclass of Sprite. And again, Sprite is brought in, it's imported, right up top here. So AS3 sound is able to access anything, any protected members of Sprite, as well any, any public members.
Private members however are off limits. So Let's go back down to that constructor function. So every class is going to have a constructor function. And you can see that again as constructor function is named AS3_sound. And the constructor function will always be public in terms of it's access modifier. Generally what you want to do in a constructor function, is invoke another function, or series of functions within this class, to get things started.
The constructor function, is invoked, whenever the particular is first run. So then we have a number of other things here, these are all different methods. So we have a Private function here, called InitStream. We have a Protected function of id3Loaded. And what these are, are little encapsulated pieces of code, that reside within the class and can be invoked from different places within the class itself.
And sometimes, if we were to set something as, say, a Public function. It would even be able to be invoked outside of this class. So, that should give you a good idea of some of the language fundamentals involved in ActionScript 3. In term specifically of a structural object oriented approach to coding with action script.
- What is ActionScript and why should I learn it?
- ActionScript 2.0 vs. ActionScript 3.0
- Implementing external source code
- Working in Flash Professional and Flash Builder
- Understanding language fundamentals
- Working with ActionScript objects