본문 바로가기

Programming/ActionScript

[AS 3.0] 라이브러리에 있는 객체 동적으로 Attach하기


There are some small differences in syntax when dynamically attaching library objects in ActionScript 2.0 and ActionScript 3.0.

In this post, I will provide a very simplified example in both ActionScript 2.0 and ActionScript 3.0.

ActionScript 2.0 Example:

You will need to make sure the library object that you are trying to attach has been configured, to do this you need to right click on the object in the library, next select properties and then check the “Export for ActionScript” and “Export in first frame” options. Make sure to enter an Identifier name for the library object; the Identifier name is used to reference the library object in when you try to use it in the ActionScript code.

In this example we will use “myClip” as the Identifier name.

Below is the ActionScript 2.0 code that will dynamically create 5 instances of the myClip object.

12345678910111213
var numberOfClips:Number = 5; var xStart:Number = 0; var yStart:Number = 0; var xVal:Number = xStart; var xOffset:Number = 2; for (var i=0;i<numberofclips;i++) {      this.attachMovie("myClip","myClip"+i,this.getNextHighestDepth());      this["myClip"+i]._y = yStart;      this["myClip"+i]._x = xVal;      xVal = this["myClip"+i]._x + this["myClip"+i]._width + this.xOffset;      this["myClip"+i].label_txt.text = i.toString(); }

ActionScript 3.0 Example:

You will need to make sure the library object that you are trying to attach has been configured, to do this you need to right click on the object in the library, next select properties and then check the “Export for ActionScript” and “Export in first frame” options. Make sure to enter an Class name for the library object; the Class name is used to reference the library object in when you try to use it in the ActionScript code. You don’t need to worry about the Base class, Flash will set a default Base class.

If you are attempting to attach a custom class that you created, you will need to use the Class name of the your custom class; you may also need to set the Base class if your custom class is derived from another custom class that you have created.

When you check the “Export for ActionScript” option, you will likely get a warning telling you that there is no class file found; for this basic example you can just click OK.

Below is the ActionScript 3.0 code that will dynamically create 5 instances of the myClip object.

123456789101112131415
var numberOfClips:Number = 5; var xStart:Number = 0; var yStart:Number = 0; var xVal:Number = xStart; var xOffset:Number = 2; for (var i:Number=0;i<numberofclips;i++) {      var mc:myClip = new myClip();      mc.name = "myClip"+(i+1);      this.addChild (mc);      mc.y = yStart;      mc.x = xVal;      xVal = mc.x + mc.width + this.xOffset;      mc.label_txt.text = (i).toString(); }

Here are the example source files.


출처 : http://www.digitaldogbyte.com/2008/07/02/dynamically-attaching-library-objects-in-actionscript-30/