Adding linked items from library to scrollpane AS3
Monday, October 6th, 2008Ok so you have been coding in AS2 for sometime and you decide to make the move over to AS3. For some people this is/was proving to be very difficult but i can honestly say with a few months of getting to grips with AS3 it’s not that bad. One of the main hurdles i had to over come was figuring out how to do things in AS3 that i would normally do without thinking about in AS2. An example of this would be adding linked items from the library to a MovieClip(mc) or scrollpane component.
AS2 Code:
-
var bob = this.createEmptyMovieClip("bob", this.getNextHighestDepth());
-
for (i=0; i<result_lv .forumCount; i++) {
-
// Attach a thread MC to the canvas
-
bob.attachMovie("Forum Forum","forum"+i,i);
-
bob["forum"+i]._x = 0;
-
bob["forum"+i]._y = nextY;
-
// Set forum details
-
bob["f"+i].forumTitle.text = result_lv["f"+i+"forumTitle"];
-
// Place next forum after this one
-
nextY += Math.ceil(bob["forum"+i]._height);
-
}
You can see where this code in use at here
When i first started with AS3 a few months back this was one of the first things i was looking to be able to do. At the time i couldn’t find any information about how to accomplish attaching items from the library to the scrollpane. Information on-line and in the doc’s only pointed to being able to load images or sws’ into the scrollpane. So early experiments resulted in using the datagrid. I have now figured out how to add linked items from library to scrollpane. Below is example code and attached source fla example.
AS3 Code:
-
// How many mc's attach to the scrollpane
-
var mcCount:int = 10;
-
// y value for next object to be placed
-
var nextY:int = 0;
-
// counter for()
-
var i:int = 0;
-
//Create a movieclip (mc) to hold the attached items
-
var holder:MovieClip = new MovieClip();
-
//Attach the holder mc to the stage
-
addChild(holder);
-
for (i=0; i<mccount ; i++) {
-
//get a new item instance from the library
-
var barMC:bar = new bar();
-
//set the y value
-
barMC.y = nextY
-
// Apply some text to the textfield
-
barMC.txTitle.text ="Instance: mc"+i
-
//attach the newly created instance to the holder
-
holder.addChild(barMC);
-
//set y value for next item
-
nextY+=barMC.height+1
-
}
-
//Attach the holder mc and items to the scrollpane
-
aSp.source = holder;






