4 Replies Latest reply on Feb 2, 2008 10:07 AM by Newsgroup_User

    Custom button class

    Level 7
      Hello,

      I am creating a button class, but the problem is that when I add a
      TextField, the hand cursor doesn't show, I've tried with MouseChildren
      with no results.

      I also want to know if this is the best way to create a custom button, I
      only need three states and a label on the button, here's the code:

      package {
      //import flash.display.DisplayObjectContainer;
      import flash.display.Sprite;
      import flash.display.SimpleButton;
      import flash.display.Loader;
      import flash.events.Event;
      import flash.net.URLRequest;

      import flash.text.TextField;
      import flash.text.TextFormat;
      import flash.text.TextFieldAutoSize;

      public class Boton extends Sprite {
      private var _boton:SimpleButton;
      private var _etiqueta:Sprite;
      private var Home:Object;
      private var formato:TextFormat;
      private var texto:TextField;
      private var mensaje:String;
      private var alineacion:String;
      private var textoCreado:Boolean = false;
      private var ancho:Number;
      private var alto:Number;

      public function Boton(Level:Object, txt:String) {
      Home = Level;
      mensaje = txt;
      _etiqueta = new Sprite();
      _etiqueta.buttonMode = false;
      _etiqueta.mouseChildren = false;

      _boton = new SimpleButton();
      _boton.upState = cargarImagen('boton_normal.png');
      _boton.overState = cargarImagen('boton_over.png');
      _boton.downState = cargarImagen('boton_down.png');
      _boton.hitTestState = _boton.upState;

      addChild(_boton);
      }

      protected function cargarImagen(img:String):Sprite {
      var picLoader:Loader = new Loader();
      var imagen:String = img;
      var request:URLRequest = new URLRequest(imagen);
      picLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,
      imagenCargada);
      picLoader.load(request);
      var carg:Sprite = new Sprite();
      carg.addChild(picLoader);
      return carg;
      }

      private function imagenCargada (ev:Event) {
      if (!textoCreado) {
      textoCreado = true;
      ancho = _boton.width;
      alto = _boton.height;
      crearEtiqueta();
      }
      }
      public function crearEtiqueta(posX:int = 0, posY:int = 0) {
      formato = Home.formato_boton;
      texto = new TextField();
      texto.autoSize = TextFieldAutoSize.CENTER;
      texto.selectable = false;
      texto.embedFonts = true;
      texto.antiAliasType = "advanced";
      texto.defaultTextFormat = formato;
      texto.x = posX;
      texto.y = posY;
      texto.width = 100;
      texto.text = mensaje;
      texto.selectable = false;
      texto.x = (ancho - texto.width) / 2;
      texto.y = (alto -texto.height) / 2;
      _etiqueta.addChild(texto);
      addChild(_etiqueta);
      }
      }
      }


      Best regards!
      Hugo.