Placing An Object On the Stage Using ActionScript 3.0

OK, to set this tutorial up I’ve created a movie clip of a circle that I named simply "Circle". I have not placed the circle anywhere on the stage using Flash, I have nothing but a blank stage.

step 1

The first thing we need to do is export the movie clip for use in our actionscript. Right-click on your movie clip in the library and click Linkage.

step 2

In the dialog box that opens, click the Export for ActionScript option. Just leave the other options as they are, this will create a class named "Circle" for us to use in our code. I generally like to leave the class name the same as the movie clip for easy reference. Click OK to close this dialog box when you are finished..

step 3

After hitting OK, you will likely see this warning:

step 4

Just click OK here. Flash is just letting us know that it couldn’t find a class for "Circle", so it will create one automatically. Next thing I have done is create a new layer to put our actionscript on. This is not a necessary step, but it’s just something a lot of us do help keep everything organized. Click inside of frame 1 of the actions layer to make sure that we add our actionscript to that frame. You can see below that frame 1 is selected.

step 5

Now open up your actions window. You can use the F9 key to open it as a shortcut if it’s not already open. Use the following actionscript code to place a circle on the stage.

1
2
3
4
var circle:Circle = new Circle();
circle.x = 10;
circle.y = 10;
addChild(circle);

A quick explanation of the code, line by line:

Line 1: This line creates a new instance of the "Circle" class, and to keep it simple I called it "circle". You could call this instance anything you want, so long as the class is "Circle."

Line 2: This line sets the x property of the circle to 10, which places the circle 10 pixels from the left of the start of the stage.

Line 3: This sets the y property of the circle to 10, placing it 10 pixels from the top of the stage.

Line 4: This line adds the circle to the display list. It is this line of code that makes Flash render the circle.

Press ctrl + enter to test your movie within Flash. This is what my movie looks like:

step 6

And that’s all there is to it! Next time I’ll show you how to make this example interactive and bring an object onto the screen when a user clicks a button. Click below to download the source file for my movie to look at what I did if you are having problems. (Right-click -> Save As)

Download Source

5 Responses to “Placing An Object On the Stage Using ActionScript 3.0”

  • Hello,

    I am contacting you on behalf of www.flashcomponents.net . I’ve read one of your tutorials and I like the way you write. Our site is continuously growing and we recently added a tutorial section. We kindly ask for your approval to allow us to publish your tutorials on our site, mentioning you as the author.
    Of course, we are inviting you to do it yourself, but either way, it would be our pleasure to publish them.

    Kind regards,
    Mike | Flash Components Team

  • Brent

    Hey Mike,

    I appreciate the feedback and interest, however I respectfully decline your offer to publish the tutorials at your site, as I’d rather keep them as original content here without any duplicate entries elsewhere. I really like the layout of your site though, nice design!

  • Hotte

    Hi Brent

    If I use the script,
    and would like a button onrelease to remove that movie. What would the script for the button look like?
    Any help be great.
    Thx!

  • Thanks for this! Just what I was looking for :D

  • grakie

    it doesn’t work, nooooooooooooooooooooooooooo!

Trackbacks

  •