A Flash Developer Resource Site

Results 1 to 5 of 5

Thread: array, loop and getURL

  1. #1
    Member
    Join Date
    Oct 2003
    Posts
    36

    array, loop and getURL

    I have an array of URL's Id like to add to some MC's with getURL.

    The MC's are called 1, 2, ,3 etc and are in an mc with an instance name of 'dock'

    Im trying to use the following code, which all appears to work, except its not replacing the final urls[i] with the corresponding urls from the array.

    Code:
    urls = [
    "http://url.com/1",
    "http://url.com/2", 
    "http://url.com/3", 
    "http://url.com/4", 
    "http://url.com/5", 
    "http://url.com/6", 
    "http://url.com/7"
    ];
    
    for (i=1; i<=7; i++) { 
    
    trace(urls[i]);/*just checking */
    
    	_root.dock[i].onRelease = function() {
    
    		getURL(urls[i]); 
    	};
    }
    Please le me know what im missing ...

    Thanks
    Ben

  2. #2
    Senile member! :)
    Join Date
    Dec 2001
    Location
    Saunaswamp (transl)
    Posts
    2,296
    Double posting...
    Last edited by Mirandir; 06-29-2005 at 05:11 AM.

  3. #3
    Senile member! :)
    Join Date
    Dec 2001
    Location
    Saunaswamp (transl)
    Posts
    2,296
    urls[i] will only be valid as long as the loop runs and not when the onRelease event fires.

    Instead put the url in a variable on the movie clip and use that instead.

    Code:
    urls = [
    "http://url.com/1",
    "http://url.com/2", 
    "http://url.com/3", 
    "http://url.com/4", 
    "http://url.com/5", 
    "http://url.com/6", 
    "http://url.com/7"
    ];
    
    for (i=1; i<=7; i++) { 
        _root.dock[i].targetURL = urls[i]
        _root.dock[i].onRelease = function() {
             getURL(this.targetURL); 
        };
    }
    /Mirandir

  4. #4
    Member
    Join Date
    Oct 2003
    Posts
    36
    thank you mirandir.

    Thats was the last litle bit to fix, so i can publish now

    thanks again.
    Last edited by benihana; 06-29-2005 at 05:43 AM.

  5. #5
    Member
    Join Date
    Apr 2004
    Posts
    78
    It's difficult to see what you are trying to do, but getURL() is just going to try open a browser window.

    Are you trying to add a text-type URL link to a movie clip? Or does each URL represent the address of a movie clip that you are trying to load into your dock movie clip?

    Either way, you should look at how you would build and code it for just one instance, then make a loop that'll do all of that seven times.

    Here's a method of making a menu of text links:

    Code:
    for (var i=0; i<urls.length; i++) {
    	dock_mc.menu_mc.btn.duplicateMovieClip("btn"+i, i);
    	dock_mc.menu_mc["btn"+i]._y = dock_mc.menu_mc.btn._y+i*int(dock_mc.menu_mc.btn._height) +i;
    	dock_mc.menu_mc["btn"+i].txt = urls[i];
    	dock_mc.menu_mc["btn"+i].hit.onRelease = function() {
    		getURL("urls[i]", _blank);
    		}
    It gets complex because you need 7 dynamic text boxes, 7 buttons and everything needs an instance name. In the example above, a single working movie clip instance is duplicated, stacked, and instansiated.

    I hope this helps.

    Len

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