0

I would like to write Actionscript loop that involves "getURL". However, from what I can see getURL does not allow concatenation of variable names?

I have variables textholder0, textholder1, textholder2 that have movieclip names as values and link0, link1, link2 that have website addresses as values.

I can use this["textholder" + 0].onRelease but getURL("link"+ 0) gives "undefined"

textholder0.onRelease = function()
{   
    getURL(link0);
}

textholder1.onRelease = function()
{
    getURL(link1);
}
textholder2.onRelease = function()
{
    getURL(link2);
}

Any way to do this so I can create a loop for the above?


Here is a test. Unfortunately, it still gives me "undefined/" for the URL. To keep it simple I created three movie clips, with instances textholder0, textholder1, textholder2. Put a loop on the main timeline.

var links:Array = ["http://www.google.ca", "http://www.google.com", "http://www.google.ru"];

for(var i:Number=0; i<links.length; i++){
    this["textholder" + i].linkURL = links[i];
    this["textholder" + i].onRelease = function() { 
        getURL(linkURL); 
    }    
}

Here is output from debugger window

Variable _level0.links = [object #1, class 'Array'] [
    0:"http://www.google.ca",
    1:"http://www.google.com",
    2:"http://www.google.ru"   ] 
Variable _level0.i = 3 
Movie Clip: Target="_level0.textholder0" 
Variable _level0.textholder0.linkURL = "http://www.google.ca" 
Variable _level0.textholder0.onRelease = [function 'onRelease'] 
Movie Clip: Target="_level0.textholder1" 
Variable _level0.textholder1.linkURL = "http://www.google.com" 
Variable _level0.textholder1.onRelease = [function 'onRelease'] 
Movie Clip: Target="_level0.textholder2" 
Variable _level0.textholder2.linkURL = "http://www.google.ru" 
Variable _level0.textholder2.onRelease = [function 'onRelease']

I am starting to think that you can not use onRelease within a loop at all.

2 Answers 2

0

getURL("link"+ 0) will try to go to a URL "link0", since "link"+ 0 will be concatenated to the string "link0", and not get the value of link0. But you can try doing this:

getURL(this["link" + 0]);

The difference, and the mechanism of the bracket notation, is that you can reference a property of an object in two ways - using dot notation, like this.link0, or the bracket notation, this["link0"]. But it has to be expressed as an object property, just saying "link" + 0 anywhere, like in getURL("link"+ 0) won't give a reference to link0.

Sign up to request clarification or add additional context in comments.

1 Comment

by using the line above getURL(this["link" + 0]); I get "undefined"
0

ok, so I think the problem with the loop here is that it was incrementing "i" variable before any of the buttons were clicked.

http://www.senocular.com/flash/tutorials/faq/#loopfunctions

Senocular.com says "you need to define a new, unique variable to represent that value at the time of function creation and have the function reference that value"

So the loop goes as following

var links:Array = ["http://www.google.ca", "http://www.google.com", "http://www.google.ru"];

var curr_button;

for(var i=0; i<=links.length; i++){
    curr_button = this["textholder"+i];
    //note creation of an extra variable "num" below to store the temp number 
    curr_button.num = i;
    curr_button.onRelease = function() { 
        getURL(links[this.num]); 
    }    
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.