Skip to main content
Inspiring
September 7, 2011
Answered

for (k=0; k<fontN.length; k++){

  • September 7, 2011
  • 1 reply
  • 941 views

There are 6 buttons and If this code is the only code in the fla why isn't the trace showing results? How can I make trace (fontN) show which button I clicked??

var fontN:Array = [Tx_btn1, Tx_btn2, Tx_btn3, Tx_btn4, Tx_btn5, Tx_btn6]; //buttons

var fontF:Array = [format1, format2, format3, format4, format5, format6];

for (k=0; k<fontN.length; k++){

    fontN.ivar=k;

fontN.onRelease = function() {

trace (fontN) // it allways shows '_level0.Tx_btn6'

my_txt.setNewTextFormat(fontF[this.ivar]);

trace (fontF[this.ivar]) // 'undefined'

}

}

This topic has been closed for replies.
Correct answer kglad

(i changed ivar to kvar.  it's just a variable name but i like to use something suggestive:  namely the kvar and k variable are related.  because i use i in most of my for-loops, most of my code uses ivar.)

anyway:


var fontN:Array = [Tx_btn1, Tx_btn2, Tx_btn3, Tx_btn4, Tx_btn5, Tx_btn6]; //buttons

var fontF:Array = [format1, format2, format3, format4, format5, format6];

for (k=0; k<fontN.length; k++){

    fontN.kvar=k;  // if you're using simple buttons, this won't work.  use movieclip buttons.

fontN.onRelease = function() {

trace (fontN[this.kvar]) // k will be the last value in your for-loop because you're clicking after the for-loop completes

my_txt.setNewTextFormat(fontF[this.kvar]);

trace (fontF[this.kvar]) // 'undefined' if format1, etc undefined

}

}

1 reply

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
September 7, 2011

(i changed ivar to kvar.  it's just a variable name but i like to use something suggestive:  namely the kvar and k variable are related.  because i use i in most of my for-loops, most of my code uses ivar.)

anyway:


var fontN:Array = [Tx_btn1, Tx_btn2, Tx_btn3, Tx_btn4, Tx_btn5, Tx_btn6]; //buttons

var fontF:Array = [format1, format2, format3, format4, format5, format6];

for (k=0; k<fontN.length; k++){

    fontN.kvar=k;  // if you're using simple buttons, this won't work.  use movieclip buttons.

fontN.onRelease = function() {

trace (fontN[this.kvar]) // k will be the last value in your for-loop because you're clicking after the for-loop completes

my_txt.setNewTextFormat(fontF[this.kvar]);

trace (fontF[this.kvar]) // 'undefined' if format1, etc undefined

}

}

Inspiring
September 8, 2011

Thanks for the explanation, the problem was with the simple buttons used instead of the movieclips buttons.

kglad
Community Expert
Community Expert
September 8, 2011

you're welcome.

p.s.  the only benefit to using simple buttons over movieclip buttons is to save a 2 minutes of coding for rollovers/rollouts.  the benefits of using movieclip buttons over simple buttons is substantial.