7 Replies Latest reply on Sep 21, 2010 12:28 PM by Flex harUI

    Referencing Flex stage through Flex Component made in Flash

    wilsonsilva7 Level 1

      Hello. I'm developing a Flex Component in Flash CS4, named Navigator. The Navigator is just a slider (I will use the slider to zoom onto something). It consists of a MovieClip with a child MovieClip named "magnifier" which is the slider handle. Here's the Navigator class:


      package {
        
      import flash.display.MovieClip;
          i
      mport flash.events.Event;
          import flash.events.MouseEvent;
          import flash.geom.Rectangle;
          import mx.flash.
      UIMovieClip;
         
         
      public class Navigator extends UIMovieClip {
            
      private var _dragging:Boolean;
             
      private var _zoom:Number;
             
      public  var _bounds:Rectangle;
             
              public function Navigator() {
                  _dragging = false;
                  _bounds = new Rectangle(68,22.5,100,0);
                  _zoom = 0;
                  this.magnifier.buttonMode = true;
                  this.magnifier.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); 
                
      stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
              }
             
             private function startDragging(event:Event):void {
                  this.magnifier.startDrag(false,_bounds);
                  _dragging = true;
              }
             
              private function stopDragging(event:Event):void {
                  if (_dragging == true) {
                      this.magnifier.stopDrag();
                      _dragging = false;
                      _zoom =((magnifier.x-68)*100)/(168-68);
                      dispatchEvent(new Event("zoomChanged"));
                  }
              }
             
              public function get zoom():Number {
                  return _zoom;
              }
          }
      }

       

      This code works on Flash but in Flex I keep gettin this error "TypeError: Error #1009: Cannot access a property or method of a null object reference."- on that red line. Probably because it is referencing the Flex's stage and not Flash's stage. This is my Flex MXML Component:

       

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Canvas xmlns:mx="http://www.adobe.com/2006/mxml" width="220" height="64" creationComplete="init()">
          <mx:Script>
              <![CDATA[
                
      import flash.display.MovieClip;
                 import mx.core.UIComponent;
                 
                
      private var ui:UIComponent = new UIComponent();
                
      private var navigator:Navigator = new Navigator();
                 
                
      public function init():void {
                      navigator.addEventListener("zoomChanged", onZoom);
                      ui.addChild(navigator);
                     this.addChild(ui);
                  }
                 
                 
      public function onZoom(event:Event):void {
                      trace(navigator.zoom);
                  }
              ]]>
          </mx:Script>
      </mx:Canvas>

        • 1. Re: Referencing Flex stage through Flex Component made in Flash
          wilsonsilva7 Level 1

          God! I spen't a lot of time coloring the code for nothing The "red line" I'm talking about is this line:

           

          stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);

          • 2. Re: Referencing Flex stage through Flex Component made in Flash
            wilsonsilva7 Level 1

            Maybe a stage reference is not what I really need. I just need some container to add the MOUSE_UP event listener. I'm just trying to make a simple slider .__.

            • 3. Re: Referencing Flex stage through Flex Component made in Flash
              Flex harUI Adobe Employee

              Flex apps are initialized off-stage.  I think you can access the stage via

              systemManager.stage, but it is better to add the mouseup listener in your

              mouseDown handler.

              1 person found this helpful
              • 4. Re: Referencing Flex stage through Flex Component made in Flash
                wilsonsilva7 Level 1

                thanks for your answer. I've already tried to use the SystemManager. I tried to "import mx.managers.SystemManager;" but I keep getting this error: 1172: Definition mx.managers:SystemManager could not be found. I think it is only available in Flex, not in Flash.


                private function startDragging(event:Event):void {
                     this.magnifier.startDrag(false,_bounds);
                     _dragging = true;    
                }

                 

                Here's my problem, visually:

                parenting.png

                eventing.png

                • 5. Re: Referencing Flex stage through Flex Component made in Flash
                  Flex harUI Adobe Employee

                  Then I would definitely recommend changing to add the mouseUp listener in

                  the mouseDown handler.

                  1 person found this helpful
                  • 6. Re: Referencing Flex stage through Flex Component made in Flash
                    wilsonsilva7 Level 1

                    Do you mean instead of having this:

                     

                            public function Navigator() {
                                _dragging = false;
                                _bounds = new Rectangle(68,22.5,100,0);
                                _zoom = 0;
                                this.magnifier.buttonMode = true;
                                this.magnifier.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); 
                                this.plus_btn.addEventListener(MouseEvent.CLICK, increaseZoom);
                                this.minus_btn.addEventListener(MouseEvent.CLICK, decreaseZoom);
                                stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
                            }
                           
                            private function startDragging(event:Event):void {
                                this.magnifier.startDrag(false,_bounds);
                                _dragging = true;
                            }

                     

                    Have this?:

                     

                            public function Navigator() {
                                _dragging = false;
                                _bounds = new Rectangle(68,22.5,100,0);
                                _zoom = 0;
                                this.magnifier.buttonMode = true;
                                this.magnifier.addEventListener(MouseEvent.MOUSE_DOWN, startDragging); 
                                this.plus_btn.addEventListener(MouseEvent.CLICK, increaseZoom);
                                this.minus_btn.addEventListener(MouseEvent.CLICK, decreaseZoom);
                            }
                           
                            private function startDragging(event:Event):void {
                                this.magnifier.startDrag(false,_bounds);
                                _dragging = true;

                              stage.addEventListener(MouseEvent.MOUSE_UP, stopDragging);
                            }

                     

                    If so, I would still have the same problem. The Flash's stage object doesn't exist in Flex.

                    • 7. Re: Referencing Flex stage through Flex Component made in Flash
                      Flex harUI Adobe Employee

                      The stage object is the same for Flash and Flex.  It just isn't available in

                      Flex until after APPLICATION_COMPLETE or ADDED_TO_STAGE.  And in general, it

                      is better not to add listeners like MOUSE_UP until needed.