5 Replies Latest reply on Jun 12, 2006 8:18 AM by Newsgroup_User

    Instanciate a customized button

    Level 7
      Hello

      I want to attach several buttons at runtime. These buttons have to had
      several attributes and methods.

      I have created a new class that extends the button class:

      class MyButton extends Button
      {
      public function MyMethod():Void
      {
      trace("Estas en mi m�todo");
      }
      }

      I have created a new symbol called symbol_btn and I have linked it to
      action script as symbol_btn. I have set MyButton as the AS 2.0 Class
      field for this symbol.

      In runtime I attach the button:

      var oneButton:MyButton;

      oneButton = _root.attachMovie("symbol_btn","abutton_btn",
      _root.getNextHighestDepth());
      oneButton.MyMethod();

      But this doesn't work. The MyMethod method doesn't show any output.
      The same happens if I add some attributes to the MyButton class and then
      try to set/get from the instance:

      oneButton.myAttribute = 5;
      trace(oneButton.myAttribute);

      The screen output is "undefined".

      If I try this example with a MovieClip symbol instead of a Button one it
      works like a charm.

      Am I doing something wrong in the button example?

      Thanks
        • 1. Instanciate a customized button
          Wolf_van_Ween Level 1
          Actually you should receive syntax errors. Do you have Actionscript 2.0 selected in Publish Settings/Flash?
          There's a mistake. The return value of the attachMovie function is of type MovieClip, not MyButton.
          Two ways around it: either use the abutton_btn instance name or cast the return value. See attached code
          Good luck
          Wolf
          PS and if you want to use new attributes (not defined in the class), you need to use the "dynamic" keyword before the class definition: dynamic class MyButton extends Button
          PPS The MovieClip class is defined as dynamic
          • 2. Re: Instanciate a customized button
            Level 7
            Wolf van Ween escribi�:
            > Actually you should receive syntax errors. Do you have Actionscript 2.0
            > selected in Publish Settings/Flash?

            Yes. I have checked that I have selected actionscript 2.0 in publish
            settings.

            > There's a mistake. The return value of the attachMovie function is of type
            > MovieClip, not MyButton.

            I also tested it using eval to get the instance of the button attached:

            btn_name = "myButton_btn";
            _root.attachMovie("symbol_btn",btn_name,_root.getNextHighestDepth());
            abutton_btn=eval("_root."+btn_name);
            abutton_btn.MyMethod();
            abutton_btn.myAttribute = 5;
            trace(abutton_btn.myAttribute);

            I have tried to cast the object to my new button class but it doesn't
            work neither. Nevertheless I haven't got any syntax error.

            It only works if I do the following:

            Create a class that extends MovieClip: class MyButton extends MovieClip
            Create a new MovieClip symbol with the above class: mybutton_btn
            Attach the symbol with attachmovie
            Then I can get the instance with eval(btn_name) or oneButton =
            attachMovie(....

            It's a bit annoying because I want to add a Button but I thing the
            MovieClip will work. I need to handle the onRelease event and the
            MovieClip object has this event.

            Thanks.



            > Two ways around it: either use the abutton_btn instance name or cast the
            > return value. See attached code
            > Good luck
            > Wolf
            >
            >
            > _root.attachMovie("symbol_btn","abutton_btn",_root.getNextHighestDepth());
            > abutton_btn.MyMethod();
            > abutton_btn.myAttribute = 5;
            > trace(abutton_btn.myAttribute);
            >
            > oneButton =
            > MyButton(_root.attachMovie("symbol_btn","abutton_btn",_root.getNextHighestDepth(
            > )));
            > abutton_btn.MyMethod();
            > abutton_btn.myAttribute = 5;
            > trace(abutton_btn.myAttribute);
            >
            • 3. Re: Instanciate a customized button
              Level 7
              Wolf van Ween escribi�:
              > There's a mistake. The return value of the attachMovie function is of type
              > MovieClip, not MyButton.

              I have found that it is not posibble to extend the Button class, but I
              don't know why :-(

              http://www.actionscript.org/forums/showthread.php3?t=72215

              Nevertheless, I can do what I want with a MovieClip

              Thanks for your help
              • 4. Instanciate a customized button
                Wolf_van_Ween Level 1
                No, not true, of course you can extend the Button class. Like any other class.

                What you can't do is assign a AS2 class to a button symbol. When you create a new symbol (f8 key) you need to specify whether it's a movieClip, a button or a graphic symbol, right? Here you need to select movieClip and not button, because otherwise the class assignment doesn't work (maybe a bug?). But this is just an issue of the development environment.

                By all means keep your "extends Button".

                Actually, I've tested the exact code I gave you, with your class definition (extends Button) and it works. It also works if you extend movieClip.

                Cheers,
                Wolf

                PS in the end it doesn't matter. A movieClip does pretty much the same things a button does, except the (anyway very limited) built-in "over" and "down" handlers. Once you start programming classes, you might want to change that default behaviour anyway. And the movieClip class offers much more flexibility to start with...
                • 5. Re: Instanciate a customized button
                  Level 7
                  Wolf van Ween escribi�:
                  > No, not true, of course you can extend the Button class. Like any other class.
                  > The thread you show doesn't say you can't, just that the guy has the same
                  > problem you have.

                  You are right.

                  > What you can't do is assign a AS2 class to a button symbol. When you create a
                  > new symbol (f8 key) you need to specify whether it's a movieClip, a button or a
                  > graphic symbol, right? Here you need to select movieClip and not button,
                  > because otherwise the class assignment doesn't work (maybe a bug?). But this is
                  > just an issue of the development environment.

                  Ok.

                  > Actually, I've tested the exact code I gave you, with your class definition
                  > (extends Button) and it works. It also works if you extend movieClip.

                  Yes. It works if I specify that the symbol is a MovieClip.

                  Thank you very much.

                  Regards.