Dynamic Text and Loading Variables From a Text File
|
|
- This code will read variables into the Flash movie from 3 text files using a loadVars object we create, then display a list of the values with an exported font if desired.
- The movie has 4 frames. In each of the first three frames a list of variables is loaded, pausing until each is loaded before moving to the next frame. The last frame has the code for creating the lists.
- This is done because when Flash starts reading in the data it does not wait for it to complete before continuing to play. Therefore it can reach the placement code before having anything to place.
- The code reads a list creating and placing text until it comes across the last variable with the value "EOF" meaning "End Of File". If not at the last list, the code will then read the next list and repeat.
- The variables in the text files are named like memberlist1, memberlist2, etc. The numbering does not have to be in order, the list will be sorted alphabetically. Any missing numbers will be ignored.
- The variable numbered "1" is reserved for the title of the list if desired. If not present the code will skip writing a title for the current list.
- Use a text editor to create the lists so no formatting is done to it. Use "&" to separate the variables. Use no spaces or line breaks:
directorlist1=Directors&directorlist2=DeathW1sh&directorlist5=||[o_o]||&directorlist7=Prophet&directorlist8=InTr3PiD&directorlist10=EOF
|
Code in frame 1:
stop();
statusText.text="Loading leaderlist";
dataClip1=new LoadVars();
dataClip1.load("leaders.txt");
dataClip1.onLoad=function()
{
play();
}
Code in frame 2:
stop();
statusText.text="Loading directorlist";
dataClip2=new LoadVars();
dataClip2.load("directors.txt");
dataClip2.onLoad=function()
{
play();
}
Code in frame 3:
stop();
statusText.text="Loading memberlist";
dataClip3=new LoadVars();
dataClip3.load("members.txt");
dataClip3.onLoad=function()
{
statusText.text="FlashingTheNet.com dynamic text tutorial";
play();
}
Code in frame 4:
stop();
function initVars()
{
leaderList="leaderlist";
directorList="directorlist";
memberList="memberlist";
titleText="DoA Members";
movieWidth=720;
movieHeight=400;
mainFontSize=26;
titleFontSize=mainFontSize+4;
subtitleFontSize=mainFontSize+2;
textboxWidth=mainFontSize*6;
titleboxWidth=mainFontSize*7.5;
titleboxHeight=titleFontSize+2
textboxHeight=mainFontSize+2;
xStart=((movieWidth/2)*-1)+15;
xPos=xStart;
yPos=((movieHeight/2)*-1)+15;
xMove=textboxWidth+5;
listDrop=mainFontSize+2;
titleDrop=subtitleFontSize+3;
xLimit=xStart+(movieWidth-textboxWidth-15);
clipLevel=0;
sortArray=new Array();
}
function makeText(member, clipLevel, xPos, yPos, textStyle)
{
this.clipLevel++;
if(member!=undefined)
{
myformat = new TextFormat();
myformat.type = "dynamic";
myformat.font = "NoControlFont";
this.createEmptyMovieClip("clip"+clipLevel, clipLevel);
this["clip"+clipLevel].createTextField("listTextbox"+clipLevel, clipLevel, xPos, yPos, textboxWidth, textboxHeight);
this["clip"+clipLevel]["listTextbox"+clipLevel].text=member;
this["clip"+clipLevel]["listTextbox"+clipLevel].embedFonts=true;
this["clip"+clipLevel]["listTextbox"+clipLevel].multiline=0;
this["clip"+clipLevel]["listTextbox"+clipLevel].wordWrap=0;
this["clip"+clipLevel]["listTextbox"+clipLevel].border=0;
this["clip"+clipLevel]["listTextbox"+clipLevel].borderColor=0xFF0000;
if(textStyle=="titleStyle")
{
this["clip"+clipLevel]["listTextbox"+clipLevel]._width=titleboxWidth;
this["clip"+clipLevel]["listTextbox"+clipLevel]._height=titleboxHeight;
myformat.color=0x666666;
myformat.size=titleFontSize;
myformat.bullet=0;
}
else if(textStyle=="subTitleStyle")
{
this["clip"+clipLevel]["listTextbox"+clipLevel]._width=titleboxWidth;
this["clip"+clipLevel]["listTextbox"+clipLevel]._height=titleboxHeight;
myformat.color=0xBBBBBB;
myformat.size=subtitleFontSize;
myformat.bullet=1;
}
else if(textStyle=="mainStyle")
{
myformat.color=0xff1100;
myformat.size=mainFontSize;
}
this["clip"+clipLevel]["listTextbox"+clipLevel].setNewTextFormat(myformat);
this["clip"+clipLevel]["listTextbox"+clipLevel].setTextFormat(myformat);
}
}
function buildArray(dataClip, whichList)
{
arrayPosition=0;
listPosition=0;
while(dataClip[whichList+listPosition]!="EOF")
{
if(listPosition>1)
{
member=dataClip[whichList+listPosition];
if(member!=undefined)
{
sortArray[arrayPosition]=member;
arrayPosition++;
}
}
listPosition++;
}
sortArray.sort(1);
}
function createList()
{
buildArray(clipNum, whichList);
if(clipNum[whichList+1]!=undefined)
{
textStyle="subTitleStyle";
member=clipNum[whichList+1];
makeText(member, clipLevel, xPos, yPos, textStyle);
yPos+=titleDrop;
textStyle="mainStyle";
}
textStyle="mainStyle";
x=0;
while (sortArray.length>x)
{
member=sortArray[x];
if(xPos>xLimit)
{
xPos=xStart;
yPos+=listDrop;
}
if(member!=undefined)
{
makeText(member, clipLevel, xPos, yPos, textStyle);
xPos+=xMove;
}
x++;
}
xPos=xStart;
yPos+=listDrop;
while(sortArray.length>0)
{
sortArray.pop();
}
}
function placeLists()
{
initVars();
textStyle="titleStyle";
header=titleText;
xPos=xStart+(movieWidth-titleboxWidth-20);
makeText(header, clipLevel, xPos, yPos, textStyle);
xPos=xStart;
clipNum=dataClip1;
whichList=leaderList;
createList();
clipNum=dataClip2;
whichList=directorList;
createList();
clipNum=dataClip3;
whichList=memberList;
createList();
}
placeLists();
delete initVars;
delete makeText;
delete buildArray;
delete createList;
delete placeLists;
delete dataClip1;
delete dataClip2;
delete dataClip3;
|