Skip to main content
Inspiring
September 19, 2007
Question

how to pass variables inside onRelease = function() ?

  • September 19, 2007
  • 4 replies
  • 356 views
Hi all
I am creating MCs through a loop, but I want to assign an onRelease function to each created MC, in
that same loop. And I need to use the variables from the loop into the function...

So I tried this:

for(var sVar in flashVars){
i++;
var mcName = sVar.substr(0,3);

// create the MCs:
duplicateMovieClip(_root.testBut,"medium"+sVar,i);
_root["medium"+sVar].testText.text=sVar;

// assign the function to each created MC:
_root["medium"+sVar].onRelease = function(){
_root[mcName]._x=0;
}
}

The problem is that the variable "mcName" is not passed into the function. How do I solve this?



--
seb ( ---@webtrans1.com)
http://webtrans1.com | high-end web design

An Ingenious WebSite Builder: http://sitelander.com
This topic has been closed for replies.

4 replies

Inspiring
September 20, 2007
My mistake. When using a for-in loop to access an array the var (in this case sVar) is always a String. However it can return a number or a string value depending on what its looping thru. So if flashVars is an Object then you don’t need to use the square bracket unless you need to access the value of that property. Which is where I am confused on what it is you want.
Inspiring
September 19, 2007
myIP wrote:
> or perhaps more ideal;
>
> for(var sVar in flashVars)
> {
> i++;
> //var mcName = sVar.substr(0,3);
>
> // create the MCs:
> duplicateMovieClip(_root.testBut,"medium"+sVar,i);
> _root["medium"+sVar].testText.text=sVar;
> _root["medium"+sVar].mcName = sVar.substr(0,3);
>
>
> // assign the function to each created MC:
> _root["medium"+sVar].onRelease = function()
> {
> trace(this.mcName)
> _root[mcName]._x=0;
> }
> }
>

thanks but this does not work.
I think that the problem is that the variables defined in the for loop do not exist in the scope of
the function.

when the MC is clicked, and the onRelease function says:

_root[mcName]._x=0;

the variable "mcName" is empty.




--
seb ( ---@webtrans1.com)
http://webtrans1.com | high-end web design

An Ingenious WebSite Builder: http://sitelander.com
Inspiring
September 19, 2007
In my last post mcName is a property of the new movieclip which it will output its value when the user clicks on that instance. There are 2 things that I notice that may be causing your code not to behavior as desired; one, is that I see no value for ‘i’. And the second is that sVar is returning a Number not a string. So you need to use [ ] to access the element in the flashVars. Also I am assuming flashVars is an Array, if not I am not sure how you are planning to retrieve info with the code you provided.
Below is updated code, and I am able to retrieve the variable mcName when I click on its movieclip. I hope that helps.
Inspiring
September 19, 2007
or perhaps more ideal;
Inspiring
September 19, 2007
Try removing the ‘_root’ from the mcName variable. So this line of code;

_root[mcName]._x=0;

…would be like this;

mcName._x=0;




But if you are looking to retrieving the instance name you can also use this;

trace(this._name)

…in the onRelease function.