2 Replies Latest reply on Oct 17, 2006 10:57 AM by leotemp

    Basic concepts with handling events.

    leotemp
      I read the docs on this but it didnt seem to address my issue, maybe i just need explained better.

      Im attempting to make a hide function, it would ideally fade the component to alpha 0.0 and then set it to visible=false. the problem is i cant set it to invisible until after the fade effect is over. Now i realize i could do this by creating a static effect for each component and have an "effectEnd" event in the effect to point to a function that hides the component but i want to do this dynamically. So how on earth do I add an eventListener that will trigger a dynamic function that requires an ID of the component it targets? I know i could have a public var thats an object that could be the target but that seems messy, is there a correct way of doing this that doesnt require multiple hard coded effects or a public "target" variable? here is an example:

      function componentVanish(obj:Object,dur:Number):void
      {
      var hideFade:Effect = new mx.effects.Fade();

      hideFade.addEventListener("effectEnd", componentHide(obj:Object)); //NO WORKY//

      hideFade.target = obj;
      hideFade.duration= dur;
      hideFade.play();
      }

      public static function componentHide(obj:Object):void
      {
      obj.visible= false;
        • 1. Re: Basic concepts with handling events.
          peterent Level 2
          Event handlers set with ActionScript addEventListener can only accept Event-type arguments. The second parameter to addEventListener is a function reference, what you have given is the result of a function execution - that's what that doesn't work.

          In your particular case, this should work:

          obj.addEventListener("effectEnd", componentHide);

          function componentHide( event:flash.events.Event ) : void
          {
          event.target.visible = false;
          }

          There is however, an easier way to do this:

          var hideFade:Effect = new Fade();
          hideFade.duraction = dur;
          obj.setStyle("hideEffect", hideFade);
          obj.visible = false;

          What I've done is set obj's hideEffect to be this specific hideFade effect. This will play when you set obj's visible property to false - thus hitting 2 targets with 1 shot.
          • 2. Basic concepts with handling events.
            leotemp Level 1
            For reasons i dont understand niether of those solutions worked, when i used them exactly as you have them posted here i get the same result as i did before, the component fades in and never turns invisible. Problem one is that it fades in, i cant make it fade out as alphaFrom and alphaTo are rejected as valid properties(??) problem two is that the visible function still doesnt seem to be triggered by the "effectEnd" event. package aside i would just like to get this working in any form, below is my application maybe somebody can point out the error (I have purposely commented out the alphaFrom and alphaTo lines but im not sure how i can control the effect if i cant specify these properties.

            <?xml version="1.0" encoding="utf-8"?>
            <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute">

            <mx:Canvas x="417" y="249" width="96" height="96" id="canvasTest" borderStyle="solid" backgroundColor="#ffffff" click="{componentVanish(canvasTest,2000)}">
            </mx:Canvas>

            <mx:Script>
            <![CDATA[
            // IMPORT NEEDED PACKAGES //
            import mx.effects.*;

            public function componentVanish(obj:Object,dur:Number):void
            {
            //ISTANTIATE FADE COMPONENT//
            var hideFade:Effect = new mx.effects.Fade();
            //CREATE EVENT LISTENER FOR NEW EFFECTS "effectEnd" EVENT//
            hideFade.addEventListener("effectEnd", componentHide);
            //SET METHODS FOR NEW FADE EFFECT//
            //hideFade.alphaFrom = 1.0; //UNIDENTIFIED PROPERTY!//
            //hideFade.alphaTo = 0.0; //UNIDENTIFIED PROPERTY!//
            hideFade.target = obj;
            hideFade.duration = dur;
            //EXECUTE EFFECT//
            hideFade.play();
            }
            public function componentHide(event:flash.events.Event):void
            {
            event.target.visible = false; //DOES NOT APPEAR TO BE EXECUTED//
            }

            ]]>
            </mx:Script>

            </mx:Application>