7 Replies Latest reply on Aug 28, 2009 2:34 AM by Whitetimer

    Confused

    Whitetimer Level 1

      Hi All

       

      I have this code  which is inside a Custom Component.  It does not work, if i do a trace statement inside the if statement on the  onOverFrame() method, there is no output.  No event is being passed.  But if i use this same code in a test page at the root level, it works perfectly.

       

      Any suggestions as to why this would be the case ?

       

      Many thanks

          <mx:Script>
              <![CDATA[
                  
                  // IMPORTS
                  import flash.net.URLLoader;
                  import flash.net.URLRequest;
                  // VARIABLES
                  
                  // METHODS
                  private function loadTA():void
                  {
                      var loader:URLLoader = new URLLoader();
                      var request:URLRequest = new URLRequest("Assets/Text/About.txt");
                      loader.load(request);
                      loader.addEventListener(Event.COMPLETE, onLoadText);
                  }
                  
                  private function btnINIT():void
                  {
                       ABOUTSCROLLDOWN.addEventListener(MouseEvent.MOUSE_OVER, scrollDownOver );
                       ABOUTSCROLLDOWN.addEventListener(MouseEvent.MOUSE_OUT, scrollDownOut );
                       ABOUTSCROLLUP.addEventListener(MouseEvent.MOUSE_OVER, scrollUpOver );
                       ABOUTSCROLLUP.addEventListener(MouseEvent.MOUSE_OUT, scrollUpOut );                 
                  }
                  
                  private function onLoadText(event:Event):void
                  {
                      ABOUTTA.text = event.currentTarget.data;
                  }
        
                   private function onOverFrame(e:Event) :void
                   {
                      if(ABOUTSCROLLDOWN.hitTestPoint(mouseX, mouseY))
                      {
                           ABOUTTA.verticalScrollPosition++;
                      }
                      if(ABOUTSCROLLUP.hitTestPoint(mouseX, mouseY))
                      {
                           ABOUTTA.verticalScrollPosition--;
                      }               
                   }
                   
                   private function scrollDownOver(e:MouseEvent):void
                   {
                       addEventListener( Event.ENTER_FRAME, onOverFrame);
                   }
                   
                   private function scrollDownOut(e:MouseEvent):void
                   {
                       removeEventListener(Event.ENTER_FRAME, onOverFrame);
                   }
                   
                   private function scrollUpOver(e:MouseEvent):void
                   {
                       addEventListener(Event.ENTER_FRAME, onOverFrame);
                   }
                   
                   private function scrollUpOut(e:MouseEvent):void
                   {
                       removeEventListener(Event.ENTER_FRAME, onOverFrame);
                   }                  
              ]]>
          </mx:Script>
      
          <mx:VBox id="ABOUTVB" width="800" height="550" styleName="myBox" horizontalAlign="center" verticalAlign="middle" horizontalScrollPolicy="off" verticalScrollPolicy="off">
              <mx:LinkButton id="ABOUTSCROLLUP" width="200" height="30" label="[  Scroll Up  ]" />
              <mx:HRule width="700" />
              <mx:TextArea id="ABOUTTA" width="700" height="400" verticalScrollPolicy="off" selectable="false" styleName="myText" />
               <mx:HRule width="700" />
               <mx:LinkButton id="ABOUTSCROLLDOWN" width="200" height="30" label="[  Scroll Down  ]" />
          </mx:VBox>
      
        • 1. Re: Confused
          christopher.mina Level 1

          Where do you call btnInit() function.  Without calling this, your subsequent event listeners never get added.

          • 2. Re: Confused
            Whitetimer Level 1

            Hi

             

            The btnInit() function gets called on the creationComplete() on custom component root.

             

            Many thanks

            • 3. Re: Confused
              flex2008 Level 3

              The component is 'created' only if you add it in another component.That is why you are not getting the traces.

              • 4. Re: Confused
                JeffryHouser Level 4

                The code just strikes me as wrong.

                 

                How do you know that the mouse will still still be over during the next EnterFrame event?

                 

                Would it be better to add the listener for the EnterFrame event, and then inside the handler check to see the mouse's location?

                • 5. Re: Confused
                  Whitetimer Level 1

                  Hi Jeffry

                   

                  I'm a relative noob to AS3 and the code was suggested by someone else as previously i had been using a timer.  With your suggestion, how would i go about coding that way ?

                   

                  Many Thanks

                  • 6. Re: Confused
                    Flex harUI Adobe Employee

                    Are you sure you added the component to a parent somewhere?

                     

                    Alex Harui

                    Flex SDK Developer

                    Adobe Systems Inc.

                    Blog: http://blogs.adobe.com/aharui

                    • 7. Re: Confused
                      Whitetimer Level 1

                      Hi .. Yes the component is added into a Viewstack at the Root level of the application ... !  This is what i have done in the end and it works great.  If you think there is a way to tweak it further, i'd love to know a better approach ...

                       

                      <mx:VBox
                           xmlns:mx="http://www.adobe.com/2006/mxml"
                           xmlns:Comp="UWTH.Components.*"
                          xmlns:UI="UWTH.UI.*"
                          width="100%" height="100%"
                          label="About Us"
                          initialize="loadText(event)"
                          creationComplete="initBTN(event)">
                          
                          <mx:Script>
                               <![CDATA[
                                    
                                  // IMPORTS //
                                  import flash.events.MouseEvent;
                                  import flash.events.Event;
                                  // VARIABLES //
                                  private var loader:URLLoader = new URLLoader();
                                  private var request:URLRequest = new URLRequest("Assets/Text/About.txt");
                                  // METHODS //
                                  private function loadText(event:Event):void
                                  {
                                      loader.load(request);
                                      loader.addEventListener(Event.COMPLETE, onComplete);
                                  }
                                  
                                  private function onComplete(event:Event):void
                                  {
                                      aboutText.text = event.currentTarget.data;
                                  }
                                  
                                  private function initBTN(event:Event):void
                                  {
                                       aboutScrollUpBtn.addEventListener(MouseEvent.MOUSE_OVER, onOverUpBtn);
                                       aboutScrollUpBtn.addEventListener(MouseEvent.MOUSE_OUT, onOutUpBtn);
                                       aboutScrollDownBtn.addEventListener(MouseEvent.MOUSE_OVER, onOverDownBtn);
                                       aboutScrollDownBtn.addEventListener(MouseEvent.MOUSE_OUT, onOutDownBtn);
                                  }
                                  
                                  private function onOverDownBtn(event:Event):void
                                  {
                                       addEventListener(Event.ENTER_FRAME, scrollDown);
                                  }
                                  
                                  private function onOutDownBtn(event:Event):void
                                  {
                                       removeEventListener(Event.ENTER_FRAME, scrollDown);
                                  }
                                  
                                  private function onOverUpBtn(event:Event):void
                                  {
                                      addEventListener(Event.ENTER_FRAME, scrollUP);
                                  }
                                  
                                  private function onOutUpBtn(event:Event):void
                                  {
                                      removeEventListener(Event.ENTER_FRAME, scrollUP);
                                  } 
                                  
                                  private function scrollDown(event:Event):void
                                  {
                                       aboutText.verticalScrollPosition++;
                                  }
                                  
                                  private function scrollUP(event:Event):void
                                  {
                                       aboutText.verticalScrollPosition--;
                                  }          
                                    
                               ]]>
                          </mx:Script>
                          
                              <mx:VBox id="aboutVBox"
                              width="100%" height="100%"
                              styleName="myBox"
                              horizontalAlign="center"
                              verticalAlign="middle"
                              verticalScrollPolicy="off"
                              horizontalScrollPolicy="off">
                              
                                   <mx:LinkButton id="aboutScrollUpBtn" width="600" label="[ SCROLL UP ]" textAlign="right" />
                                   <mx:Spacer height="5" />
                                   <mx:HRule width="600" />
                                   <mx:HRule width="400" />
                                   <mx:HRule width="200" />
                                   
                                   <mx:TextArea id="aboutText" width="600" height="400" styleName="myText" verticalScrollPolicy="off" />
                                   
                                   <mx:HRule width="200" />
                                   <mx:HRule width="400" />
                                   <mx:HRule width="600" />
                                   <mx:Spacer height="5" />
                                   <mx:LinkButton id="aboutScrollDownBtn" width="600" label="[ SCROLL DOWN ]" textAlign="right" />
                              
                              </mx:VBox>
                           
                      </mx:VBox>
                      
                      

                       

                      Whitetimer