2 Replies Latest reply on Dec 1, 2009 4:24 PM by jerryhamby

    Using SerialElement with static vars

    jerryhamby

      ========== The Goal ====================

      To get access to the MediaPlayer's element properties and methods, such as (numChildren, getChildAt, etc…)

      My goal is to build a Image Viewer (using left and right arrows) that uses SerialElement as the source of the images.

      Please note the class where all this lives, is a Singelton-type class using static vars.

       

      ========== Code Snippet ====================

      (using Sprint 7 on CS4)

      public static var pPlayer:MediaPlayer;

      public static var pImageSerialElement:SerialElement;

      .

      .

      pPlayer = new MediaPlayer();

      .

      .

      pImageSerialElement = new SerialElement();

      pImageSerialElement.addChild(new ImageElement(new ImageLoader(),new URLResource(new URL("images/image1.jpg"))));

      pImageSerialElement.addChild(new ImageElement(new ImageLoader(),new URLResource(new URL("images/image2.png"))));

      pImageSerialElement.addChild(new ImageElement(new ImageLoader(),new URLResource(new URL("images/image3.png"))));

      .

      .

      pPlayer.element=pImageSerialElement;

      trace("pImageSerialElement = " + pImageSerialElement);     //// [object SerialElement]

      trace("pImageSerialElement.numChildren = "+pImageSerialElement.numChildren);  //// 3

      trace("pPlayer.element = " + pPlayer.element);       //// [object SerialElement]

      trace("pPlayer.element.numChildren = " + pPlayer.element.numChildren);    ////// this is where I get the error

       

      ========== The Error ====================

      1119: Access of possibly undefined property numChildren through a reference with static type org.osmf.media:MediaElement.

      Even though I can see the data in Debug mode, I can't get pass the error message.

       

      What am I doing wrong?

       

        • 1. Re: Using SerialElement with static vars
          bringrags Level 4

          I assume by "error" you mean a compile error:  you're trying to access numChildren through a MediaElement variable rather than a SerialElement variable, which won't work.  You could work around this by casting to SerialElement:

           

          trace("pPlayer.element.numChildren = " + SerialElement(pPlayer.element).numChildren);

           

          Can I ask why you need to access numChildren?  The point of SerialElement is you set it up, then interact with it as if it were a MediaElement.

          • 2. Re: Using SerialElement with static vars
            jerryhamby Level 1

            Thanks for reply:

            I wanted to create a Image viewer that could be manipulated using a controller (you know, next and previous buttons).
            I wanted to see if I could use the SerialElement as the data source, instead of building an array. The numChildren would give
            me the count of the SerialElement.
            FYI, I wasn't interested in using TemporalProxyElement cause I'm not building a slideshow at the moment.
            I have mostly figured out things in the code below:
               public static function mPlayImageSeq(vImageToPlay:Number):void {
            pPlayer.element = null;
            pMediaSprite = new Sprite();
            AppMaster.pMediaPlaceHolder.addChild(pMediaSprite);
            pPlayer.addEventListener(LoadableStateChangeEvent.LOADABLE_STATE_CHANGE, mImageLoadableStateChange );
            pPlayer.addEventListener(MediaPlayerCapabilityChangeEvent.VIEWABLE_CHANGE, mViewable );
            pMediaSprite.addEventListener(MouseEvent.CLICK , mImageClicked );
            pImageSerialElement = new SerialElement();
            pImageSerialElement.addChild(new ImageElement(new ImageLoader(),new URLResource(new URL("images/image1.jpg"))));
            pImageSerialElement.addChild(new ImageElement(new ImageLoader(),new URLResource(new URL("images/image2.png"))));
            pImageSerialElement.addChild(new ImageElement(new ImageLoader(),new URLResource(new URL("images/image3.png"))));
            if (vImageToPlay > 998){
               pPlayer.element=pImageSerialElement;
            }else{
               pPlayer.element=pImageSerialElement.getChildAt(vImageToPlay);
            }

             

              trace("pPlayer.element = "+pPlayer.element);
              // trace("pPlayer.element.numChildren = "+pPlayer.element.numChildren);
              trace("pImageSerialElement.numChildren = "+pImageSerialElement.numChildren);
              trace("pImageSerialElement.getChildAt(0) = "+pImageSerialElement.getChildAt(0))
            }