views:

201

answers:

2

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?

+1  A: 

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.

AnthonyWJones
+1  A: 
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]);
evilpenguin
The problem is that I'm going to pass the urls as FlashVars with swfObject. Any thoughts on how I would do that?
mofle
I edited the reply in order to fit your question, please check above.
evilpenguin
I can't get it to work. Can you take a look at my code?HTML: http://pastebin.com/m50660adaActionscript: http://pastebin.com/m1e0c177
mofle
Your actionscript code doesn't seem to include the lines I suggested in the edit of my reply. Please make sure you give me the link to the most recent piece of code you wrote and make sure you include the two lines from the above answer.
evilpenguin
Sorry, gave you the wrong version. Here's the right one: http://pastebin.com/m2f39f5f4
mofle
That's simple, my bad. urlArray.push(urls.split(",")[i]) should turn into urls.push(urls.split(",")[i]). Got a little confused while pasting from the code I tested to the site. Let me know how it goes.
evilpenguin
And there was one more error in naming variables. I updated the two lines of code in the reply above, make sure you get the latest ones.
evilpenguin
Still doesn't work. Nothing happens when I click on the links.
mofle
Try separating any flashVars problems from any other issues. Define the urlVars string on the first line of code, as urlVars="link1,link2,.." and then compile in debugging mode, so you can listen to variables and trace() as much as you like. Let me know how it goes.
evilpenguin
There is something wrong with the array loop because i tried tracing "urls" after the loop and it is just undefined. If i trace urlVars before the array loop I get the content. Any thoughts?
mofle
Very wierd. Only way I can help is if you can upload the full source and post the link, I can look over it in a couple of hours or so. Make sure you include everything, though.
evilpenguin
Why at all use this strange 'for'? Be simpler!var urls : Array = (urlVars) ? urlVars.split(',') : [];
dragonfly
Ah, great truth you speak, my friend. :)
evilpenguin
Yes, it almost worked. The trace output I got was www.2.comwww.3.comwww.4.comwww.5.comwww.6.comwww.7.comwww.8.comwww.9.comwww.10.comwww.11.comwww.12.comundefined It seems like it is not getting the first one. The project file: http://drop.io/ro7ip1s
mofle
If you find any other stuff in the code that could be improved. Let me know ;)
mofle
That makes sense now. You see, your for goes from 1 to length, but in order to properly read from the array, you should go from 0 to length-1. Just modify SetMouseAction(...,urls[i]);into SetMouseAction(...,urls[i-1]);
evilpenguin
Now everything works. Thanks for all your help. Really appreciate it ;)
mofle