Adding linked items from library to scrollpane AS3

Ok 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:

  1. var bob = this.createEmptyMovieClip("bob", this.getNextHighestDepth());
  2. for (i=0; i<result_lv .forumCount; i++) {
  3. // Attach a thread MC to the canvas
  4. bob.attachMovie("Forum Forum","forum"+i,i);
  5. bob["forum"+i]._x = 0;
  6. bob["forum"+i]._y = nextY;
  7. // Set forum details
  8. bob["f"+i].forumTitle.text = result_lv["f"+i+"forumTitle"];
  9. // Place next forum after this one
  10. nextY += Math.ceil(bob["forum"+i]._height);
  11. }

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:

  1. // How many mc's attach to the scrollpane
  2. var mcCount:int = 10;
  3. // y value for next object to be placed
  4. var nextY:int = 0;
  5. // counter for()
  6. var i:int = 0;
  7. //Create a movieclip (mc) to hold the attached items
  8. var holder:MovieClip = new MovieClip();
  9. //Attach the holder mc to the stage
  10. addChild(holder);
  11. for (i=0; i<mccount ; i++) {
  12.  //get a new item instance from the library
  13.  var barMC:bar = new bar();
  14.  //set the y value
  15.  barMC.y = nextY
  16.  // Apply some text to the textfield
  17.  barMC.txTitle.text ="Instance: mc"+i
  18.  //attach the newly created instance to the holder
  19.  holder.addChild(barMC);
  20.  //set y value for next item
  21.  nextY+=barMC.height+1
  22. }
  23. //Attach the holder mc and items to the scrollpane
  24. aSp.source = holder;

This entry was posted in Flash AS3, Flash CS3 components and tagged , , , . Bookmark the permalink.

4 Responses to Adding linked items from library to scrollpane AS3

  1. puppy says:

    To add to above advice, if the only content you want to add is a text field, you’re going to have to add an empty shape behind it, and draw textWidth/textHeight-sized transparent rectangle in there. This probably was the case in AS2 too.

  2. Goule says:

    Thx bro. really need this.

  3. HeppCat says:

    Fantastic tutorial. Exactly what I needed.

    One question — again, something that seemed easier in AS2:
    How would you reference one of the barMC children inside the aSp scrollPane’s holder for, let’s say, a function for an onRollover MouseEvent? Prior to asking this question, I’ve tried many different things but continue to receive errors such as “Access of possibly undefined property…” etc., when I know everything has been defined already.

    Thanks in advance and, again, great tutorial.

  4. Paul Ferrie says:

    You could add an custom eventListener() to the scrollPane that would list for a custom event dispatched from the mc that is in the scrollPane. Here is some good reading on the subject of dispatching custom events. http://www.learningactionscript3.com/2007/11/20/dispatching-custom-events/