Ghost9
Ghost9

Reputation: 259

Stop and start a for loop in AS3?

I'm pulling an xml and using a for loop to create a thumb list. This list is going to be quite long but I only 25 thumbs to be loaded at a time, so that the next 25 is only loaded when a user hits a button. I know how to set up a for loop in a function, but I can't quite figure out how to break up a loop where it would stop and start. I was thinking I would call the function each time a button is pressed and the loop would pick up where it left off with the next 25.

I thought maybe I could substite other variables into the for(); but everything I've tried breaks it. I tried pulling the var i:int = 0; out of the for so the function could set the i, but I guess I'm not clear on exactly how the for loop works.

What I'm doing:

function loadarticleHeadlines():void
{
    for (var i:int = 0; i < egarticleXml.articlelist.articleitem.length(); i++)
    {
        vsThumb = new articleBox();
        vsThumb.alpha = 0;
        vsThumbLoader = new Loader();
        vsThumbLoader.load(new URLRequest(egarticleXml.articlelist.articleitem[i].articlethumbnail));
        articleListContainter.addChild(vsThumb);
        vsThumb.articleImage.addChild(vsThumbLoader);
        vsThumb.articleTitle.text = egarticleXml.articlelist.articleitem[i].articletitle;
        titleAutosize(vsThumb.articleTitle);
        vsThumb.x = next_x;
        next_x += 260;
        articlevsThumb[i] = vsThumb;
        //vsThumbLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, showBox);
        vsThumb.clickBtn.buttonMode = true;
    }
}

Upvotes: 0

Views: 3001

Answers (1)

user1385191
user1385191

Reputation:

Try this out.

var xml:Array = [];
for(var i:int=0;i<100;i++)
{
    xml.push(i);
    //array from 0-99
}
var xmlPosition:int = 0;
grabXML(xmlPosition);
//25
grabXML(xmlPosition);
//50
grabXML(xmlPosition);
//75
grabXML(xmlPosition);
//100

function grabXML(position:int):void
{
    for(position;position<xml.length;position++)
    {
        trace(xml[position]);
        //yields 0-24, 25-49, 50-74, and 75-99
        if(position === xmlPosition + 25)
        {
            break;
        }
    }
    xmlPosition += 25;
}

I'm breaking as soon as the parameter is 25 more than its original value (xmlPosition). Calling the function additional times will yield nothing, as xmlPosition is greater than xml's length property.

Upvotes: 1

Related Questions