6 Replies Latest reply on Feb 3, 2010 5:08 AM by archemedia

    issue geting to data I need on an event

    bentwonk

      Hi, new to flex so making some stupid mistakes, I am having trouble getting to the data I require from an event.

       

      basically,I create my own button class, which uses a simple button, and stores an id number passed to it, eg:

       

          private var id:Number;

       

          public function GroupButton(idIn:Number,commander:String)
           {
                  button = new SimpleButton;

                id=idIn

           }

       

           public function getID():Number{return id;}

       

       

      I then create some of these buttons ins another class and attach an event handler too them (I found the event has to be attached to the buton component to work) eg:

       

                          private var groupButtons:Array = new Array();

       

              for(var i:int=0; i<numberOfGroups; i++)       {

                      var s:String="img"+(i+1);

                      var t:GroupButton = new GroupButton(i,s);

                      t.doubleClickEnabled = true;

                      groupButtons[i]=t;

                      groupMenu.addChild(t.button);

                      groupButtons[i].button.addEventListener(MouseEvent.MOUSE_OVER, GroupButtonOver);

                      groupButtons[i].button.addEventListener(MouseEvent.DOUBLE_CLICK, GroupDoubleClick);

                  }

       

      the event triggers fine on mouse over, but how do I get to the id number the button was generated with?

       

      I have tried:

       

          private function GroupButtonOver(event:MouseEvent):void
          {
                       var t:Number= event.target.getID();
          }

       

      and any varient I can think off, all cause an error.

       

       

      also while I am here,  the doubleClick event never triggers :-( or I am very slow on the mouse button.

      As always any help much appriciated

        • 1. Re: issue geting to data I need on an event
          bentwonk Level 1

          update: double click works now, t.button.doubleClickEnabled = true;

          • 2. Re: issue geting to data I need on an event
            archemedia Level 4

            Try this:

             

               private var groupButtons:Array = new Array();

             

                    for(var i:int=0; i<numberOfGroups; i++)       {

                            var s:String="img"+(i+1);

                            var t:GroupButton = new GroupButton(i,s);

                            t.doubleClickEnabled = true;

                            t.addEventListener(MouseEvent.MOUSE_OVER, GroupButtonOver);

                            t.addEventListener(MouseEvent.DOUBLE_CLICK, GroupDoubleClick);

                            groupButtons[i]=t;

                            groupMenu.addChild(t);

                        }

             

            Dany

            • 3. Re: issue geting to data I need on an event
              bentwonk Level 1

              hmm, compiles, runs, but events don't seen to fire.

               

              I did change the line

               

              groupMenu.addChild(t);

               

              to

               

              groupMenu.addChild(t.button);

               

              as in the first case the buttons were not rendered.

              • 4. Re: issue geting to data I need on an event
                archemedia Level 4

                ok, there are other things still going on.

                 

                You GroupButton class, is it actionScript class or mxml component?

                 

                can you post your GroupButton class?

                 

                d

                • 5. Re: issue geting to data I need on an event
                  bentwonk Level 1

                  Actionscript class.

                   

                  thanks for your help, here is the whole thing:

                   

                  package com.WWII.MapManagement
                  {
                      import flash.display.BitmapData;
                      import flash.display.Loader;
                      import flash.display.SimpleButton;
                      import flash.display.Sprite;
                      import flash.events.Event;
                      import flash.events.IOErrorEvent;
                      import flash.geom.Matrix;
                      import flash.net.URLRequest;

                   

                      public class GroupButton extends Sprite
                      {
                          public var button:SimpleButton;
                          private var id:Number;
                         
                          private var numButtonWidth:Number = 100;
                          private var numButtonHeight:Number = 50;
                         
                          private var url:String;
                          private var loader:Loader = new Loader();

                   

                          public function GroupButton(xOff:Number,commander:String)
                          {
                              button = new SimpleButton;
                              id=xOff;
                              button.x=xOff*66;
                              
                              url = "assets/images/commanders/"+commander+".png";
                              var request:URLRequest = new URLRequest(url);
                             
                              loader.load(request);
                              loader.contentLoaderInfo.addEventListener(Event.COMPLETE, InitGroupButton);
                              loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
                          }


                          public function getID():Number
                          {
                              return id;
                          }
                         
                            private function InitGroupButton(event:Event):void
                            {
                              button.upState = drawButtonState(0xDAD8F3);
                              button.overState = drawButtonState(0x4F42C6);
                              button.downState = drawButtonState(0xDDF2FF);
                              button.hitTestState = drawButtonState(0xDDF2FF);
                              button.useHandCursor = true;
                          }
                         
                          private function drawButtonState(rgb:uint):Sprite
                          {
                              var sprite:Sprite = new Sprite();
                             
                              var myBitmap:BitmapData = new BitmapData(loader.width, loader.height, false);
                   
                              myBitmap.draw(loader, new Matrix());
                             
                              var matrix:Matrix = new Matrix();
                             // matrix.rotate(Math.PI/4 );
                            
                              var x:Number=1000;
                              var y:Number=400;
                              var w:Number=loader.width;
                              var h:Number=loader.height;
                                 
                              sprite.graphics.lineStyle(4,rgb,0.5);
                             
                              sprite.graphics.beginBitmapFill(myBitmap, matrix, false);
                              sprite.graphics.drawRoundRect(0,0,w,h,10,5);
                              sprite.graphics.endFill();
                             
                              return sprite;
                          }
                         
                          private function ioErrorHandler(event:IOErrorEvent):void
                          {
                              trace("Unable to load image: " + url);
                          }

                   

                      }
                  }

                  • 6. Re: issue geting to data I need on an event
                    archemedia Level 4

                    I didn't know your class extended Sprite...

                     

                    A few suggestions:

                     

                    - ...addChild(t.button) is not very cool. t.button is a property of your GroupButton class, so you shouldn't do anything with it from outside the class itself. It is good practice to keep a class 'encapsulated'.

                    - Why don't you extend Button or SimpleButton class in your GroupButton class? That way, your class would dispatch mouseOver, Click etc. events automatically, because the Button or SimpleButton classes are event dispatchers. Maybe this would simplify your class already.

                    - If you extend Button class, you have access of properties like id, name etc. Off course, you can add your own, like 'command'.

                    - Try to use getters and setters for public properties (I suppose you know what that is?)

                     

                    Let me know if I can help you further

                     

                    Dany

                    1 person found this helpful