Optimized XML-based AS3 Carousel

Here’s a quick way to optimize this AS3 carousel-style image gallery / menu.

After this change to Main.as there was no need to manually update the number of images in the carousel’s main class file. Just change drop the new images into the images folder & add their names as XML elements & you’re done.

  1. On line 29, change “private var numOfItems:Number = 3;” to “private var numOfItems:Number;”
  2. After line 58, add “numOfItems = imglist.length();” and let the code calculate how many elements are in your xml file
//...some code here

private var numOfItems:Number; //line 29
//...some code here

private function loadBottles(e:Event):void
       // pull in the xml file and load the song data
        xml = new XML(e.target.data);
        var imglist:XMLList = xml.bottleimages.@image; //line 58
	numOfItems = imglist.length();

       //some code here...


//some code here...

Since imglist is of type XMLList it has a built-in length() method which acts like a similar Array method and returns the number of items in the collection.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s