Sindre Sorhus
Sindre Sorhus

Reputation: 63478

Pass strings to function in a loop

I have this code that activates when rollover, rollout, and release. I functions for rollover and rollout works, but the release function does not. I'm trying to pass some strings with url's to the function within a loop.

var url1:String = "http://www.google.com";
var url2:String = "http://www.google.com";
var url3:String = "http://www.google.com";
var url4:String = "http://www.google.com";
var url5:String = "http://www.google.com";
var url6:String = "http://www.google.com";
var url7:String = "http://www.google.com";
var url8:String = "http://www.google.com";
var url9:String = "http://www.google.com";
var url10:String = "http://www.google.com";
var url11:String = "http://www.google.com";
var url12:String = "http://www.google.com";


function SetMouseAction(buttonMC, arrowMC, dynamicTF, linkURL):Void {
    trace(linkURL);
    buttonMC.colorText = dynamicTF;
    buttonMC.onRollOver = function() {
        TweenLite.to(arrowMC,0.5,{_x:"2", _alpha:50, ease:Back.easeOut});
        this.colorText.textColor = 0x7cb0b7;
    };
    buttonMC.onRollOut = function() {
        TweenLite.to(arrowMC,0.5,{_x:37, _alpha:100, ease:Back.easeOut});
        this.colorText.textColor = 0xffffff;
    };
    buttonMC.onRelease = function() {
        if (linkURL) {
            getURL(linkURL);
        }
    };
}

for (var i:Number = 1; i<=12; i++) {
    SetMouseAction(this["link"+i],this["arrow"+i],this["text"+i],url+1);
}

I have a strong feeling that the url+1 in the for loop is wrong, but I don't know how to do it.

Any thoughts?

Upvotes: 0

Views: 357

Answers (2)

evilpenguin
evilpenguin

Reputation: 5478

var urls:Array = new Array();
urls.push("http://link1");
...
urls.push("http://link12");

function SetMouseAction(buttonMC, arrowMC, dynamicTF, linkURL):Void {
...
}

for (var i:Number = 1; i<=12; i++) {
    SetMouseAction(this["link"+i],this["arrow"+i],this["text"+i],urls[i]);
}

Make sure that the Array urls has at least 12 elements, or else you will get an index out of bounds error.

later edit: if you need to extract the urls from flashvars, just use a separator like "," and define a string with all your urls, like so: urlVars=url1,url2,url3,...,url12

Then in order to extract the urls and push them intor the array, you use the split function:

var urls:Array = new Array();
for (var i=0; i<urlVars.split(",").length; i++) urls.push(urlVars.split(",")[i]);

Upvotes: 1

AnthonyWJones
AnthonyWJones

Reputation: 189437

Change url+1 to this["url"+i]

That'll get this code working. However you really should consider using an array called url with 12 elements rather than creating 12 individual variables.

Upvotes: 1

Related Questions