A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: Problems reading text file

  1. #1
    Member
    Join Date
    Feb 2004
    Posts
    38

    Problems reading text file

    So here's the deal. I'm making a chronological timeline with Flash MX which receives info from a text file and moves a movie clip to its position depending on its day, month, and year. On the first frame of the root level I have:

    code:
    function yearPosition(month) {
    if (month == "feb") {
    addDay = 31;
    } else if (month == "mar") {
    addDay = 59;
    } else if (month == "apr") {
    addDay = 90;
    } else if (month == "may") {
    addDay = 120;
    } else if (month == "jun") {
    addDay = 151;
    } else if (month == "jul") {
    addDay = 181;
    } else if (month == "aug") {
    addDay = 212;
    } else if (month == "sep") {
    addDay = 243;
    } else if (month == "oct") {
    addDay = 273;
    } else if (month == "nov") {
    addDay = 304;
    } else if (month == "dec") {
    addDay = 334;
    } else {
    addDay = 0;
    }
    return addDay;
    }
    loadVariablesNum("info.txt",0);



    When called upon, yearPosition will adjust the movie clip's x-position depending on its month. Each variable in the text file is named &entries, &day1, &day2, &year1, &year2, etc.

    In the next frame, on the root level, i have:

    code:

    for(i=1, i<=entries, i++)
    data.duplicateMovieClip("data"+i,i)


    This duplicates the movie clip for however many entries there are in the entries variable. One of the problems is that the variable "entries" isn't identified as a number. I tried parseInt(entries) and Number(entries) to try to convert it, but neither of those work either.

    The movie clip that gets duplicated is assigned a unique number "i" and looks for its corresponding data from the text file.

    code:

    onClipEvent(load) {
    day = _root["day"+i];
    month = _root["month"+i];
    year = _root["year"+i];
    addDay = _root.yearPosition(month);
    day += addDay;
    this._x += ((parseInt(year) - 1816)*182.5) + parseInt(day);
    this.date = _root["date"+i];
    this.event = _root["event"+i];
    }



    When this movie clip calls on the function yearPosition, it's supposed to input the month that it's assigned to. But for some reason the function doesn't recognize the month that's input. Also, I can't do mathematical operations with the variables from the text file. I tried to convert them to numbers with parseInt() and Number() but they still won't work.

    I know this is a lot of problems and a lot to read but I'd greatly appreciate someone's help on this. Thank you.

  2. #2
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    hi,

    When using parseInt() you'll want to specify the radix: 10.
    code:

    parseInt(entries, 10);


    Did you try this?

  3. #3
    Member
    Join Date
    Feb 2004
    Posts
    38
    Okay, I specified the radix this time and it evaluated the number, but it was different than what I wanted. What exactly does the radix do to the number?

  4. #4
    Registered User nunomira's Avatar
    Join Date
    May 2002
    Location
    portugal
    Posts
    7,003
    'but it was different than what I wanted'
    What was different, what happened?

    When you load variables from an external source, (text file, php script, xml file...) loaded variables are strings.
    When you need a number, you may need to convert the variable to a number. Sometimes flash does automatic type conversion. This depends on the situation and depends on the version of flash you're using.
    To play safe, you should do that conversion yourself.

    In order to convert a string to a number, you use parseInt(), as Number() may not work as you expect - depending on how the number is written - because of the way flash handles numbers.

    As you probably want to have a base 10 number, you specify the radix 10.

    You may read this two posts for some examples: 1 2.

    Just to make sure:
    Is the variable being loaded corrected? Is it already available when you try to use it in the for() loop?
    i.e., are you preloading the variables?
    Unless you're still using Flash 5, you shoud use the LoadVars Class instead of loadVariables() or loadVariablesNum().

  5. #5
    Member
    Join Date
    Feb 2004
    Posts
    38
    Okay, I figured out the problem now. Huge thanks for the help!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center