Dynamically add Movieclip to Scrollpane AS3

Step 1: Create Movieclip Title which needs to be added dynamically into

Scrollpane. Make sure you have checked “Export for Actionscript”. This will
automatically create Class name “Title”

Step2 : Inside Title Movie clip , insert texttool and an Image. Convert that
image to symbol and give instance name “Ico”

Instance name for TextTool is TxtValue

Step 3: Insert ScrollPane in Scene1 and give instance name MainScroll

Step 4 : Paste the code below in scene1 actionscript panel.

var S:MovieClip=new MovieClip()  // Create empty movieClipMainScroll.source=S // Assign empty movieclip to the scrollpane. It is mandatory to assign empty movie clicp to scrollpane before you do anything dynamically with scrollpanefor(var i=0;i<5;i++)   // Constructing a loop to create 5 movieclips dynamically
{
var T:Title=new Title() // Create the MovieClip (Title) and assign it to the variable T

T.y=i*30  // Set y axis value for the T
T.Txt.text=”Title” +i //Setting up the Txt value dynamic
T.Ico.id=i // Title movie clip already holds another movieclip named Ico  the movieclips
T.Ico.addEventListener(MouseEvent.CLICK,GetID) // Add eventlistener for that Ico .
MovieClip(MainScroll.content).addChildAt(T,i) //addChild will be used when u add anything to the movieclip. So here the scrollpane content needs to be treated as Movieclip , So MovieClip(MainScroll.Content) will be treated as Movieclip
}

function GetID(e:Event)
{
trace(e.target.id) // e.target points to the Ico movieclip.. Where in id been stored into Ico. So it gives you the respective id numbers.
}

Output

Codes Explanation

MainScroll.source=SAssign empty movieclip to the scrollpane. It is
mandatory to assign empty movie clicp to scrollpane before you do
anything dynamically with scrollpane
MovieClip(MainScroll.content).addChildAt(T,i)
addChild will be used when u add anything
to the movieclip. So here the scrollpane content needs to be treated as
Movieclip , So MovieClip(MainScroll.Content) will be treated as
Movieclip

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.