theworldsbestpies
theworldsbestpies

Reputation: 37

Flex AS3: Dynamically Create Graphic with BitmapImage Child

I've trawled the net trying to find a solution, but everything seems to be mxml-centric. What I want is to dynamically create a series of Graphics objects each with a child BitmapImage. However, this doesn't seem to work:

   var bmi:BitmapImage = new BitmapImage();
   bmi.source="@Embed('custom-case.png')";
   var gr:Graphic = new Graphic( );
   gr.addElement( bmi );
   gr.x = 50;
   gr.y = 50;
   this.addElement( gr );

Whereas, this does:

   <s:Graphic x="250" y="250">
    <s:BitmapImage source="@Embed('custom-case.png')">
    </s:BitmapImage>
   </s:Graphic>

Thanks in advance for any ideas.

Paul

Upvotes: 1

Views: 2984

Answers (2)

Simon
Simon

Reputation: 31

To follow up Shruti's comment/question (I am unable to post comment since my current reputation insufficent):

The requirement for dynamic updating of images with mxml is the same as indicated with the original answer, which is that any images you might want to dynamically change to must be pre-embeded in your mxml:

[Embed(source="image.png")] private var theImage:Class;

which can be later used to update an image source as such:

<fx:Script>
    <![CDATA[           
    [Embed(source="image.png")] private var theImage:Class;

    private function updateImage():void {
        image.source = theImage;
    }
    ]]>
</fx:Script>

<s:BitmapImage id="image" source="@Embed('defaultImage.png')"/>

Upvotes: 0

rxx
rxx

Reputation: 764

it is quite different in AS3, you have to define a variable class type like shown below.

[Embed("custom-case.png")]
 private var someImage:Class;
 ...
 bmi.source=someImage;

Upvotes: 4

Related Questions