-
functions with arrays
function does not works with array in following code:
in reference image these five are the movieclips on the stage and their instance names are same as the array items. see attachment for reference.
Code:
var mainBtnsArr:Array = ["chapters", "projects", "exercises", "skills", "glossary"]; for (i=0; i<mainBtnsArr.length; i++) { mainBtn = mainBtnsArr[i]; mainBtnsArr[i].onPress = function() { trace("working"); }
Any help please?
-
Hi,
where is the attachment?
-
-
1 Attachment(s)
Attachment 73825
sorry my fault..... now please find attachment.
-
Hi,
Here you go, you were on the right track though.
PHP Code:
var mainBtnsArr:Array = [chapters, projects, exercises, skills, glossary];
for (i = 0; i < mainBtnsArr.length; i++)
{
mainBtnsArr[i].TheNum = [i + 1];
mainBtnsArr[i].onPress = function()
{
trace(this.TheNum + " " + this._name + " : " + " Pressed");
};
mainBtnsArr[i].onRelease = mainBtnsArr[i].onReleaseOutside = function()
{
trace(this.TheNum + " " + this._name + " : " + " Released");
};
mainBtnsArr[i].onRollOver = function()
{
// do something else
};
mainBtnsArr[i].onRollOut = mainBtnsArr[i].onDragOut = function()
{
// do something else
};
}
-
or if you want to keep you Array items as Strings, you can use this:
Code:
var mainBtnsArr:Array = ["chapters", "projects", "exercises", "skills", "glossary"];
for (i=0; i<mainBtnsArr.length; i++) {
btnClick(this[mainBtnsArr[i]]);
}
function btnClick(btn){
btn.onPress = function(){
trace("working");
}
}
the key is this[mainBtnsArr[i]], because it looks for an object with the value of the string. You were only giving a String onPress handler :p
-