Skip navigation
Currently Being Moderated

How do I code thumb scroller to have images set to its individual widths?

Apr 27, 2012 8:05 AM

I am following a tutorial on active.tutsplus.com (http://active.tutsplus.com/tutorials/effects/create-a-responsive-xml-i mage-scroller-in-actionscript-3-0/comment-page-2/#comment-53078)

Tutorial is done with an assumption that all of the images in the thumb scroller are identical in height and widths. Particularly set to 140 +20 pixels.

In my case each image has a different width. How would I code this set up so the images would fit nicely with various width and have identical distances between them.

Here is the code snippet from the tutorial:

 

function buildScroller(imageList:XMLList):void{

          trace("build Scroller");

 

          for (var item:uint = 0; item<imageList.length();item++) {

                    var thisOne:MovieClip = new MovieClip();

 

                    //outline

                    var blackBox:Sprite = new Sprite();

                    blackBox.graphics.beginFill(0xFFFFFF);

                    blackBox.graphics.drawRect(-1, -1, 124, 107);

                    thisOne.addChild(blackBox);

 

                    thisOne.x = (140 +20) *item;

                    thisOne.itemNum = item;

                    thisOne.title = imageList[item].attribute("title");

                    thisOne.link = imageList[item].attribute("url");

                    thisOne.src = imageList[item].attribute("src");

 

                    //trace(thisOne.itemNum, thisOne.title, thisOne.link, thisOne.src);

 

 

                    //add item

                    scroller.addChild(thisOne);

          }

 

          trace("termination of build scroller");

 

}

 
Replies
  • kglad
    62,158 posts
    Jul 21, 2002
    Currently Being Moderated
    Apr 27, 2012 8:18 AM   in reply to nikolaig

    where are the images?  are they loaded?  are they on-stage?  are they in the library with classes assigned?

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 27, 2012 8:21 AM   in reply to nikolaig

    It looks like what you will need to do is manage the x placement differently than how it is currently done.  ...

     

    thisOne.x = (140 +20) *item;

     

    You will need to know the width of each image as you place them in order to be able to manage spacing them out evenly.  So you need to mainatin an x-placement variable that you use to plant each one.  You increase that value based on the image width and desired spacing value for the next image that will be planted... something like...

     

    thisOne.x = currentX;

     

    currentX += currentImageWidth+spaceBetween

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 27, 2012 9:34 AM   in reply to nikolaig

    No, that is not a typo, there is nothing wrong with what I showed.  You are continually increasing the currentX value.  += is a shorthand code for saying

     

    currentX = currentX + whatever

     

    You do not want to specify the var where you did, it would need to be done earlier and would possibly be just...

     

    var currentX = 0;  // or whatever the first value of x will be.

     

    So what you changed it to will not work.

     

    The names I gave to the variables you are asking about are descriptive, so you should be able to determine where you get them from.

     

    currentImageWidth is obtained from whatever image you just planted

     

    spaceBetween is whatever amount of space you want between your images.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 27, 2012 12:05 PM   in reply to nikolaig

    You seem to be close to what you might need, except you are not doing anything to acquire the width based on what you showed for the xml data. 

     

    var currentImageWidth=thisOne.src

     

    The src data from the xml is the path/name of the image file, not the width of it.  If you can include the width as a parameter in your xml then you could use that.  If you cannot do that, then you need to load the image and acquire its width after it has finished loading... THis would mean you have to change the way you are loading to be sequencial... meaning you load each image and process it thoroughly one at a time.  You don't trigger loading another until you are done with the current one.

     
    |
    Mark as:
  • Currently Being Moderated
    Apr 27, 2012 3:05 PM   in reply to nikolaig

    No, there are no standardized attributes, you can add anything you want as data in your xml.  In your case it will be easier to deal with getting the data thru the xml file.

     

    You should learn to use the trace() function to see if the data you are expecting is what you are getting... such as in

     

    thisOne.currentImageWidth = imageList[item].attribute("width");

    trace(thisOne.currentImageWidth);

     

    One other thing you need to account for... when you read in xml data you are reading String values.  Your width needs to be a Number, so you need to convert it.

     
    |
    Mark as:
  • Currently Being Moderated
    May 1, 2012 1:31 PM   in reply to nikolaig

    To convert the width String to a Number just change that one line to...

     

    thisOne.currentImageWidth = Number(imageList[item].attribute("width"));

     

    As far as the xml error goes, the problem will be something in your xml file.  The only thing that I can see that might cause it is you have a blank space in your first tag closer....

     

    ...://www.888acolyte.com" width="123"/ >

     

    should be

     

    ://www.888acolyte.com" width="123"/>

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points