PDA

View Full Version : Movieclip doesn't increase its size when children are added??


Bshed
08-20-2008, 05:13 AM
Hi, I've created a thumbnail collection class that holds (as the name suggests) all the thumbnails. I intend to use this as the source of a scrollpane so that the user can scroll down the thumbnails. Here is the code for the ThumbNailCollection class:


package
{
import flash.display.MovieClip;
import flash.xml.*;
import flash.events.*;
import flash.net.*;

public class ThumbNailCollection extends MovieClip
{
private var thumb_names:Array; // all the file names of thumbnails in this collection
private var row:int; // counter for how many rows of thumbnails in this collection
private var col:int; // counter for how many columns of thumbnails in this collection
private const START_X:Number = 25.5; // the x coordinate of the starting point
private const START_Y:Number = 33.5; // the y coordinate of the starting point
private const HORIZ_GAP:Number = 70; // the horizontal gap between thumbnails
private const VERTI_GAP:Number = 54; // the vertical gap between thumbnails
private const THUMB_W:Number = 150; // width of the thumbnail
private const THUMB_H:Number = 112.5; // height of the thumbnail

public function ThumbNailCollection(xml_name:String)
{
row = 1;
col = 1;
thumb_names = new Array();
readList(xml_name);
}

// reads the list of picture files
private function readList(file:String):void
{
var ldr:URLLoader = new URLLoader();
ldr.addEventListener(Event.COMPLETE, loadingComplete);
ldr.load(new URLRequest(file));
}

private function loadingComplete(e:Event):void
{
var pictures:XML = new XML(e.target.data);

for(var i:int = 0; i < pictures.pic.length(); ++i)
thumb_names.push(pictures.pic[i]);

for(var j:int = 0; j < thumb_names.length; ++j)
addThumbNail(thumb_names[j]);
}

public function addThumbNail(thumb:String):void
{
var t:ThumbNail = new ThumbNail(thumb);

if(row == 1 && col == 1) // no thumbnail added so far
{
t.x = START_X;
t.y = START_Y;

addChild(t);
++col;
}
else
{
t.x = START_X + (THUMB_W + HORIZ_GAP) * (col - 1);
t.y = START_Y + (THUMB_H + VERTI_GAP) * (row - 1);

addChild(t);
++col;
}

// if the column is more than 3, create another row and set col to zero
if(col > 3)
{
++row;
col = 1;
}
}
}
}


This works perfectly (I can see all the thumbnails being added in their correct positions). But when I set an instance of ThumbNailCollection as the source of my scrollpane, the scrollpane doesn't have a scroll bar. So I traced the size of the instance of ThumbNailCollection, and it turns out to be 0 * 0. I even tried to explicitly change the size of ThumbNailCollection whenver a new thumbnail is added, but then the size is still 0*0 and now I can't even see the thumbnails.

My questions are: Why doesn't the Movieclip class automatically adjust its size when a child is added? How can I make it adjust its size accordingly whenever a child is added?

I'm new to actionscript, so this may turn out to be a really easy, basic question so please be tolerable, and thanks in advance!