A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: problem with array scope

  1. #1
    Member
    Join Date
    Aug 2004
    Location
    China
    Posts
    43

    problem with array scope, need your help badly!!!!

    I have worked on this a whole day, I want to make a flash navigation which get data from a xml file. now it's almost finished, but there's a problem with array scope that makes me unable to open a url ? have tried to mark the code red where it is concerning to the problem, and uploaded the fla and xml file. I am really looking forward to your help.

    this is the AS:

    _global.myTxt = new Array();
    _global.myUrl = new Array();
    _global.news_array = new Array();
    var starXml = new XML();
    starXml.load("star3.xml");
    starXml.ignoreWhite = true;
    System.useCodepage;
    starXml.onLoad = function(success) {
    if (success) {
    trace("star.xml is loaded");
    fetchTxt();
    //this is the function used to get data form xml and make the btn mc. } else {
    trace("star.xml isn't loaded");
    }
    };
    //------------------------got XML date and put it in a array------------
    function fetchTxt() {
    for (i=0; i<starXml.firstChild.childNodes.length; i++) {
    myTxt[i] = starXml.childNodes[0].childNodes[i].attributes;
    }
    trace("myTxt's length is "+myTxt.length);
    //----------------put xml data into news_arry-----------------
    for (i=0; i<starXml.firstChild.childNodes.length; i++) {
    news_array[i] = myTxt[i].thename;
    myUrl[i] = myTxt[i].theurl;//the theurl is the variable in xml file store the url trace("the theurl in xml is "+myUrl[i]);//it trace the right url here, but it is undifined late.
    //trace("xml data is "+myTxt[i].thename);
    //trace("news_array[i] is "+news_array[i]);
    }
    //--------------------make btns----------------
    this.createEmptyMovieClip("newBtn", 100);
    newBtn._x = 0;
    newBtn._y = 5;
    for (i=0; i<news_array.length; i++) {
    this.attachMovie("btn", "newBtn"+i, 101+i);
    eval("newBtn"+i).createTextField("btn_txt", 102, 0, 0, 10100, 18);
    temp1 = eval("newBtn"+i);
    temp1.btn_txt.text = news_array[i];
    eval("newBtn"+i)._x = 0;
    eval("newBtn"+i)._y = i*16+7;
    //16是两个按钮_y的距离
    eval("newBtn"+i)._alpha = 0;
    eval("newBtn"+i).onRelease = function() {
    trace("you clicked on "+myUrl[i]);//problem is here, it always trace "you clicked on undifined", why myUrl array is undifined here? and how can I use it??)
    };
    eval("newBtn"+i).onRollOver = function() {
    this._alpha = 100;
    };
    eval("newBtn"+i).onRollOut = function() {
    this._alpha = 0;
    };

    }
    }
    Attached Files Attached Files
    Last edited by Katwalk; 04-02-2006 at 05:50 AM.
    welcome to my blog: hi.baidu.com/2rainbow

  2. #2
    All 1s and 0s dmonkey's Avatar
    Join Date
    Nov 2005
    Location
    Leeds, UK
    Posts
    606
    Hi,

    I just discussed this problem in another thread. Flash won't hard-code the values of myUrl[i] into the onRelease function. It waits until you press the button, then looks up the value of 'i'. By this time, 'i' always equals 3, so you always get the last value in myUrl (which is undefined in your xml). To fix this, assign to each button a variable that represents the value of 'i' when you created the onPress event, like this:

    code:

    eval("newBtn"+i)._alpha = 0;
    eval("newBtn"+i).gotoURL = myUrl[i]
    eval("newBtn"+i).onRelease = function() {
    //getURL(this.gotoURL, "_blank");
    trace("The URL I am supposed to go to is " + this.gotoURL);
    };
    eval("newBtn"+i).onRollOver = function() {
    this._alpha = 100;
    };


    "If I have seen further, it is by standing on the shoulders of giants." - Sir Isaac Newton

  3. #3
    Member
    Join Date
    Aug 2004
    Location
    China
    Posts
    43
    thank you so much, dmonkey!! i just posted this problem on flashkit before I left for super, but I had been thinking about it since I shut up the computer! 21:40, I came back, very happy to see that you have replied my thread, and I readed your reply carefully then fixed my flash with your instruction, it success!! I believe I will have a good dream tonight!! thanks again!!
    Last edited by Katwalk; 04-02-2006 at 10:03 AM.
    welcome to my blog: hi.baidu.com/2rainbow

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