Hi,
Another Actionscript 3 tutorial. This time explaining how can we display the library content.
Quote
The Library in Flash acts as a repository where all of your custom graphics, buttons, movie clips, external content, etc. is stored. From the Design view, accessing elements from the Library is very easy. You just drag and drop what you want onto your stage. In many situations, though, you may be required to use code to display your library items on an as-needed basis. This tutorial covers just that scenario where Library content is displayed on your stage, and you’ll learn how using Flash CS3′s ActionScript 3 language.
At the end of the tutorial, you will have created something that looks similar to the following image where the various circles were displayed on the stage using nothing but code:
If you are coming from an AS 2.0 or earlier background, one of the major changes in ActionScript (AS) 3 is that the attachMovie function is no longer supported. In the past, if you had Library content that you wanted to display on your stage, you would call the attachMovie function, pass in the Linkage name of your library item, add a few more pieces of data, and your content would be displayed on your screen. In AS3 there are some similarities to that approach, but there are some differences also.
Let’s Get Started:

[ set your animation's width/height to 300 by 200 ]

[ draw a blue, solid, filled circle ]

[ give your symbol the name circle and make sure it is also set to be a movie clip ]
Do not hit OK just yet. Let’s make some more modifications.

[ check 'Export for ActionScript and enter BlueCircle for your class ]
The Base class field will automatically be populated for you, but if it hasn’t, make sure to enter flash.display.MovieClip as shown in the above image.

[ your circle in your Library ]
If you do not see your Library, press Ctrl + L to display it.

[ your circle displayed on the top-left corner ]
Technically, that is really all there is to taking content from your Library and displaying it on your stage. There are actually a lot more things that can be done, so in the next page I will go through and provide some new tips as well as code explanations.
Adjusting the Movie Clip Properties
Right now, our circle is simply displayed in the top-left corner. It would be great if we could adjust the properties such as, for starters, the position of our circle. To do that, we need to go back to our code. Right click on the frame you added your code to and select Actions to display the Actions window with the code you pasted earlier:

[ your code window ]
Our code right now does very little. We have our Main function that adds our new BlueCircle movie clip as a child element. Notice that our BlueCircle movie clip is followed by the two parenthesis ( and ). Those parenthesis indicate that our BlueCircle movie clip is actually being treated like a class. For now, don’t worry if you are not familiar with classes.
Let’s change our Main function’s code to have a specific variable that takes care of all things related to our BlueCircle. Replace your current code with the following:
If you run your application again, nothing would have changed. Your blue circle would display in the top-left corner just like it did earlier. The main difference is that instead of simply passing in new BlueCircle() to our addChild function, you now have a variable called newCircle that stores a reference to your object of type BlueCircle.
The end result is the same, for our addChild function gets a reference to a new BlueCircle object. The only difference is that instead of passing in the object directly like we did earlier, in the revised code you are passing in a variable referencing the object instead.
By having a separate variable referencing our BlueCircle object, you have a lot more flexibility in what you can actually do with this variable. Let’s decide to place our circle at an x position of 50 and a y position of 75. The code for doing that would be:
When you add the above lines to your existing code, your modified code should look like the following:
Notice that I added the new lines before our this.addChild(newCircle) line. You do not have to do it that way. You could have placed those two lines after the addChild, and everything would have still worked.
If you preview your work now, you will notice that your circle is now located at 50, 75:

[ your circle now displayed at 50, 75 ]
Getting back to the two lines you added, notice that I am following our newCircle object with the x and y properties in order to set the circle’s x and y positions. The reason you are able to use x and y is because our BlueCircle object is actually an extension of a MovieClip.
When you first converted your blue circle into a MovieClip, you may recall seeing the Base class field in the Linkage area of your Convert to Symbol window:

[ where you automatically accepted the Base class for MovieClip ]
The Base class text tells you the source from where your new symbol will be inheriting its functionality from. In our case, the BlueCircle class is derived from the MovieClip class which can be found at flash.display.MovieClip.
To get to the punch line, this means our BlueCircle object can access all of the MovieClip class’s Events, Methods, and Properties beyond just x and y. To see that in action, replace your existing code with what you see below:
In the above code, I just extended the earlier code by including a few more modifications. The main thing to notice is that I am creating a loop that pushes 200 BlueCircle circles to your stage. Because I am randomizing a lot of the BlueCircle object’s properties, you get a random collection of circles drawn in various positions, sizes, and transparencies:

[ your final output ]
Conclusion
Taking content from your library and dynamically placing them on your stage is a very important and useful feature. In this tutorial, you learned the basic syntax for displaying the content and making modifications to it. There is actually a lot more that wasn’t covered in this tutorial.
For example, behind the scenes, a BlueCircle() class is being used and is automatically provided for you. You can instead create your own BlueCircle class file (BlueCircle.as) and create a lot of custom functionality that this tutorial did not explore. Also, one of the nice features of attachMovie is that you can pass an arbitrary number of arguments to your newly generated movie clip (more info). It is possible to replicate that feature in AS3, and future tutorials will definitely touch upon this and other related topics in greater detail.
Unquote
You can find this tutorial and more at http://kirupa.com
Brgds,
CP
| Copy and Paste the code below | |
| Email and IM | |
| Websites | |
| Forums | |
follow:
October 5th, 2007 at 9:11 pm
thank you
As a beginner with 3.0 this was extremely useful
You have written an easy to understand and functional script that I have
adapted for my needs, but better yet, I understood what and why you did it .
April 13th, 2008 at 12:04 am
Oh yeah– one suggestion, you should add some extra info on Math.random().
I had to Google to find out, but apparently Math.random() returns a value N where 0 <= N < 1. (In other words, it returns a value between 0 and 1, potentially including 0).
July 21st, 2009 at 1:26 pm
Hello
Thanks for your Tutorial.
Is it possible give different Links (ie http://www.google.com or http://flashenabledblog.com, etc..) for each circle?