Run-time instances
Attaching movie
To create movie on the stage the method attachMovie is used. It may be applied as an Action (method of the main movie as a whole) or as a method of the certain instance that appears on the stage. Let's try it- Create any movie symbol. In our example it's a red circle called mvBal
- If you worked on the symbol on the stage – delete it from the stage. It must be presented only in the library
- While in the library, place the cursor on the symbol icon and click right mouse button. Select the Linkage menu item
- Enter the identifier name. In our example it's "ball"
- Check Export for ActionScript and Export in the first frame It will allow to refer to this identifier from the code. You need to have it in the first frame because it makes possible to attach this symbol's instance without having any "seed" instance on the stage – directly from library
- Enter the following code in the first frame of the main timeline
attachMovie("ball", "b1",10);The first parameter in this Action is the linkage identifier from the library. The second is the name of new instance. The first is the depth. As you created the instance in the layer where code is placed you may wish to appear it over or under other content. The depth arranges the instance on the "Z-axis". - Run the movie. The instance of your symbol must appear in the left top corner
- Modify the movie. Create a button. Delete the code from the frame and add it to the button's event handler
on (release) { attachMovie("ball", "b1",10); }The code will do eventually the same. However you need to pay attention that in this case not the main timeline but the button is the parent of the new ball instance and attachMovie method belongs to the button. Actually it's this.attachMovie(). So if you add the code to the button even handler like this._visible = false; the button disappear when clicked…. But the ball also disappears when clicked!Always keep in mind the hierarchy when working with instances!!!