7 Replies Latest reply on Nov 20, 2009 11:41 PM by Shongrunden

    How to add a displayObject to spark containers?

    2233dddddfve43445-OovMWo

      Hi guys,

       

      This should be easy but I'm struggling with the new spark library.... I have a couple of graphic classes extending sprite or movieClip that I want to use in new spark container. Normally I had no problem putting them in mx container but I just can't find out how to fit them in Spark containers. Since the spark is the new "way to go" I would be happy not to write my classes again.

       

      I tried casting with addElement and different containers without succes. So anyone has an idea how to add diplayObjects in the new spark containers?? Or should I just keep using the mx containers??

       

      Thanks

        • 1. Re: How to add a displayObject to spark containers?
          Flex harUI Adobe Employee

          addElement should work.  What happened when you tried that?  The children have to be IVisualElements.  Before, they generally had to be IUIComponents unless you were adding children to UIComponent itself.

           

          Alex Harui

          Flex SDK Developer

          Adobe Systems Inc.

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

          1 person found this helpful
          • 2. Re: How to add a displayObject to spark containers?
            2233dddddfve43445-OovMWo Level 1

            Hi and thanks for answering

             

            This is the error I'm getting when trying to add my class (which extends Sprite) in a spark container using the addElement method.

             

            1067: Implicit coercion of a value of type flash.display:Sprite to an unrelated type mx.core:IVisualElement

             

            Before I was using addChild to UIComponent and it worked as intended. Is there a way to cast a Sprite as a IVisualElement or simething that would prevent me from writing my classes again?? I don't know the IVisualElement class. Any more ideas?

             

            Thanks

             

             

            • 3. Re: How to add a displayObject to spark containers?
              Flex harUI Adobe Employee

              Quick and dirty might be to continue to put them in a UIComponent and put the UIComponent in the Spark Container

               

              Alex Harui

              Flex SDK Developer

              Adobe Systems Inc.

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

              • 4. Re: How to add a displayObject to spark containers?
                2233dddddfve43445-OovMWo Level 1

                Well this isn't exactly what I wanted but that works thanks. I just can't believe that the new spark library won't be able to support the displayObject in their containers ?!?! That would really limit the possibility between pure actionscript/flash cs4 and flash builder. My workflow brings me a lot more often in flash cs4 and flashdevelop (where the displayObject is omnipresent) than in flash builder or flex 3.

                 

                Anyway, thanks for your help

                • 5. Re: How to add a displayObject to spark containers?
                  Peter deHaan Level 4

                  You could also try <s:SpriteVisualElement/> and addChild():

                   

                   

                  <?xml version="1.0" encoding="utf-8"?>
                  <s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
                                 xmlns:s="library://ns.adobe.com/flex/spark"
                                 xmlns:mx="library://ns.adobe.com/flex/halo"
                                 initialize="init();">
                      <fx:Script>
                          <![CDATA[
                              private const spr1:Sprite = new Sprite();
                              private const spr2:Sprite = new Sprite();
                  
                              private function init():void {
                                  spr1.graphics.beginFill(0xFF0000, 0.5);
                                  spr1.graphics.drawRect(10, 10, 100, 80);
                                  spr1.graphics.endFill();
                                  con.addChild(spr1);
                  
                                  spr2.graphics.beginFill(0x0000FF, 0.3);
                                  spr2.graphics.drawRect(20, 20, 80, 100);
                                  spr2.graphics.endFill();
                                  con.addChild(spr2);
                              }
                          ]]>
                      </fx:Script>
                      <s:SpriteVisualElement id="con" />
                  </s:Application>
                  

                   

                  Peter

                  1 person found this helpful
                  • 6. Re: How to add a displayObject to spark containers?
                    2233dddddfve43445-OovMWo Level 1

                     

                     

                    Thanks a lot Peter, exactly what I wanted since this way I'll only be using the new spark library. I just couldn't believe that there wasn't a simple way. Back to learning classes I guess

                     

                    Thanks for your help, much appreciated!

                    • 7. Re: How to add a displayObject to spark containers?
                      Shongrunden Adobe Employee

                      You could also subclass spark.core.SpriteVisualElement instead of Sprite.  In the case of MovieClip try extending mx.flash.UIMovieClip instead and it should play nice in a spark container.