ThinkGeek - Cool Stuff for Geeks and Technophiles

April 13, 2004

Button weirdness

The last time I had to work with buttons was at least two years ago, but today at work, we have received some graphic assets from a freelance designer, that has provided us with a lot of buttons.

These buttons have a textfield inside, and there are two different textfields, one for the "normal" state, and another for the "over state. So I wrote the following code to assign that dynamic fields text ( where "tf" is the textfield )

class net.designnation.UI.buttonHandler
{
 public static function setHandlers( instanceName: MovieClip, text: String )
 {
  instanceName[ "theText" ] = text;
  instanceName.onRollOver = function( )
  {
   this.tf.text = this[ "theText" ];
  }
  instanceName.onRollOut = function( )
  {
   this.tf.text = this[ "theText" ];
  }
 }
}

It didn't work. So, I tried to trace the buton contents:

class net.designnation.UI.buttonHandler
{
 public static function setHandlers( instanceName: MovieClip, text: String )
 {
  instanceName[ "theText" ] = text;
  instanceName.onRollOver = function( )
  {
   for ( var k in this )
   {
    trace( k );
   }
   this.tf.text = this[ "theText" ];
  }
  instanceName.onRollOut = function( )
  {
   this.tf.text = this[ "theText" ];
  }
 }
}

And here came my surprise. Maybe this is well know to all of you, but the result of the trace command was:

tabIndex
getDepth
enabled
useHandCursor
instance1

So, there was the textfield: instance2. But, when I rolled out, it traced "instance3". And when I rolled over again, it traced "instance4", and so on. It only happens when there are two different textfields. If there's only one textfield for all the button's states it always traces "instance1"

It was the first time I saw it, so it surprised me. By the way, the final solution was as easy as it seems ( althoguh it relies on something really weird ):

class net.designnation.UI.buttonHandler
{
 public static function setHandlers( instanceName: MovieClip, text: String )
 {
  instanceName[ "theText" ] = text;
  instanceName.onRollOver = function( )
  {
   for ( var k in this )
   {
    if  ( k.substring( 0,8 )== "instance" && k.text != undefined )
    {
     this[ k ].text = this[ "theText" ];
    }
   }
  }
  instanceName.onRollOut = function( )
  {
   for ( var k in this )
   {
    if  ( k.substring( 0,8 )== "instance" && k.text != undefined )
    {
     this[ k ].text = this[ "theText" ];
    }
   }
  }
 }
}

But, how much time will it work?. I don't know

At least, I've found another reason to not use buttons, and use movieclips instead

Posted by Cesar Tardaguila Date: April 13, 2004 07:47 AM | TrackBack
Comments

Isnt it easier to associate a variable with each button, and keep button in a movieclip?

I ususally, assosicate a variable with textfield and keep the button in a movieclip. So variable must exist in the scope of movieclip holding the button.

Or you can attach buttons, in empty movieclip an get it workin..

Not sure of your case, but i used former technique a lot lately in my Flash 5 projects, yeah we still do in Flash 5 :)

//Abdul

Posted by: Abdul Qabiz en: April 13, 2004 02:12 PM

I see, the fact is that I haven't thought about that possibility. You mean, going back to the Flash 4 way of managing texts, associating them to a variable, not giving them an instance name?

I'll give it a try

Posted by: Cesar Tardaguila en: April 13, 2004 03:27 PM

Anyway, I think we have not time enough to edit all the buttons ( about 200, and the deadline is just around the corner ), but I'll remember this for the future.

Thanks, Abdul.

Posted by: Cesar Tardaguila en: April 13, 2004 03:28 PM

Why don't you just use one textfield and change the textformat on it ?

Posted by: nick en: April 13, 2004 05:05 PM

Because of time ( or the lack of ). There are a lot of buttons, and we have no time to edit them ( we have not time even to make a jsfl to edit them for us, which was our first "solution" ).

That's why we decided to do it the way we've done it. Altought we know it's not the best.

Posted by: Cesar Tardaguila en: April 13, 2004 05:22 PM