Join Todd Perkins for an in-depth discussion in this video Connecting a Loader instance, part of Flash CS4 Professional: Building UI Elements.
In this movie, we're going to look at connecting a loader instance in our FLA file to the ProgBar class. So the first thing we're going to do is take this private property and make a public version of that using Set and Get methods. Now if you're unfamiliar with Set and Get methods, I recommend watching my title on Object Oriented Programming in Flash CS4. So let's define the Set method below the constructor. So declare the method public, function set loader. In the parenthesis, receive one parameter. We'll call it l and datatype it to a Loader, : void, no return data, and some curly braces.
Now if you're not familiar with Get and Set methods, basically, Set and Get methods work like properties. So, for example, you set the loader property in FLA file using the = sign. So ProgBar. loader equals some loader. Now what that does is it runs the set loader method. So, our advantage here is that we can run a huge block of code or as much code as we want whenever our property is set. That gives us an advantage to just having the loader property be public. That's what we're doing here.
Again, you could find more detail about Set and Get methods in my Object Oriented Programming title. So here we have our Set method and in here the first ting we'll do is set _loader = l, and then we're going to add some event listeners to our loader. So _loader. contentLoaderInfo. Remember for loaders, you don't add the event listener to the loader itself. You add the event listener to its contentLoaderInfo property. So contentLoaderInfo. addEventListener and the first one is going to be Event.OPEN. That's when the load starts and we'll run a method called loadStarted. We'll define that in just a minute. I'll add one more event listener. So just copy and paste this line of code on the next line, change the Event to Event.COMPLETE, rename the method to loadFinished in the pasted code.
Then before we define loadStarted and loadFinished, right below the Set method here we're going to define the Get method. This is for retrieving property. So public function get loader, no parameters, returns a loader, and then inside of this method just return _loader. So basically, if you wanted to, let's say, trace the value of loader from the FLA file, then the loader property will be triggered here via this Get method, since we're not setting a value, we're getting a value and we return that _loader property.
Right above the set method, I'm going to define loadStarted. So private function loadStarted. Again, this is private because we don't need to access it from the main Timeline or anywhere else. So create an event with the datatype of event, :void for no return data. When the load starts, all we want to do is show the ProgBar. So visible = true. Now just copy and paste loadStarted and then change the name to loadFinished and we'll set visible = false. That's it.
So I'll save the file and then go to the FLA and we'll connect the loader on the Timeline. So if you don't remember the instance name of the ProgBar, you can always Click Insert a target path. See right there? It's prog_mc. Cancel out. Right above loader.load, you always want to do this before you load the file, we'll set prog_mc's loader property equal to loader. That's that loader instance we created on line 1 in this code.
So we connect the loader and load the file and the progress bar does its magic. So you test the movie, you don't see anything right now, but if you test it again, you'll see the outline of the loader for a few seconds and then the image. So obviously, we're not seeing the load progress but the loader is showing and hiding at the right time and we note it's stopping on that first frame. So we've successfully connected our loader to our progress bar and we've responded it to the Event.OPEN and Event. COMPLETE events in our progress bar class.
- Applying OOP to real-world projects
- Creating customizable elements for a unique look
- Learning efficient workflow practices with ActionScript 3.0
- Developing new programming skill sets with OOP
- Reusing custom codes across several applications