[FMX]Integrate loop in function?

I made a [color=red]for in[/color] loop and a [color=red]function[/color] to control my button mc’s like this:

for (var i in this) {
 if (this*._name.substr(0, 4) == "btn_") {
  setButton(this*._name);
 }
}
function setButton(clip) {
 var clip = eval(clip);
// The rollOver, rollOut and release stuff goes here
}

Now I would like to integrate the loop in the function ! I tried the following:

function setButton(clip) {
 for (var i in this) {
  if (this*._name.substr(0, 4) == "btn_") {
   var clip = eval(this*._name);
// clip.onRollover = function(){ and the rollOut and release stuff goes here
}
}
} 
} 

but that isn’t working. Can somebody please tell me what I’m doing wrong.

Thanks in advance

I now added: [color=red]substr(4)[/color]

to the var clip part, so the complete syntax is now:

[color=red]var clip = eval(this*._name.substr(4));[/color]

but still nothing. Anybody an idea what is wrong or else any sugestions where I probably can find the answer

Thanks in advance

the eval is pointless… you have a direct clip reference from your for in loop

this[ i ] IS the clip. Theres no point in getting the clips name from THE clip and then converting it back to the clip again :wink:

is the function defined in the same timeline where those buttons exist? If so (which is right) I just suggest trying some traces in there to make sure you’re actually finding your buttons in the loop i.e. trace(this*)

It is realy strange. The trace give no result. They are on the same timeline indeed. And if I place the for in loop outsite the function they are working fine. I realy don’t understand what i’m doing wrong ?

I still haven’t figured out why the “btn_” mc’s are not working any more after I integrated the for in loop in the function. This is how I have it now:

function setButton(clip) {
for (var i in this) {
if (this*._name.substr(0, 4) == "btn_") {
var clip = this*;
// clip.onRollover = function(){ and the rollOut and release stuff goes here
}
}
} 
}

The function is on the main timeline but I the btn_ mc’s I have to control are insite another MC “menuMC”

Can somebody please help me :crying:

function setButton() {
for (var i in menuMc) {
if (menuMc*.name.substr(0, 4) == "btn") {
clip = menuMc*;
clip.onRollOver = function() {
trace(this._name);
};
}
}
}
setButton();

Thank you very much stringy :hugegrin: This is great :thumb:

welcome
Glad it worked but the other poster asked if your buttons were in the sametimeline as the code.

I know, but at that time they where. Only I changed a few things and that is why they are now integrated in that one MC. I have one more question if you don’t mind. This is my complete code:

_root.currMovie = "homede";
container.loadMovie(_root.currMovie+".swf");
function setButton() {
for (var i in menuMc) {
if (menuMc*._name.substr(0, 4) == "btn_") {
clip = menuMc*;
clip.onRollOver = function() {
	new Color(clip).setRGB(0xFFFFFF);
	trace(this._name);
};
clip.onRollOut = function() {
	if (!this.pushed) {
	 new Color(clip).setRGB(0x999999);
	} else {
	 new Color(clip).setRGB(0xFFFFFF);
	}
};
clip.onRelease = function() {
	resetKnop();
	this.pushed = true;
	new Color(clip).setRGB(0xFFFFFF);
	if (_root.currMovie == undefined) {
	 _root.currMovie = this._name.substring(4);
	 this._parent.container.loadMovie(this._name.substring(4)+".swf");
	} else if (_root.currMovie != this._name.substring(4)) {
	 if (this._parent.container._currentframe>=this._parent.container.midframe) {
	 _root.currMovie = this._name.substring(4);
	 this._parent.container.play();
	 }
	}
};
}
}
}
function resetKnop() {
for (var i in menuMC) {
if (menuMc*._name.substr(0, 4) == "btn_") {
clip = menuMc*;
clip.pushed = false;
new Color(clip).setRGB(0x999999);
}
}
}
setButton();

As you can see should the buttons change color on rollOver, rollOut and release. But that isn’t working. Nomather over which button I roll only the second one change color. What should I change to make this work again?

Thanks in advance

Problem solved :slight_smile: I changed new Color(clip) in new Color(this)

But what should I change to make this part work again:

	if (_root.currMovie == undefined) {
	 _root.currMovie = this._name.substring(4);
	 this._parent.container.loadMovie(this._name.substr  ing(4)+".swf");
	} else if (_root.currMovie != this._name.substring(4)) {
	 if (this._parent.container._currentframe>=this._parent.container.midframe) {
	 _root.currMovie = this._name.substring(4);
	 this._parent.container.play();  

Difficult to know without seeing the file. Is container on the main timeline?If so it should be this._parent._parent.container

That was indeed the solution :slight_smile: Thanks :slight_smile: