PDA

View Full Version : help with image embed problem


Roaders
03-12-2007, 09:13 AM
Hi Guys

I have this code:


<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script>
<![CDATA[
import mx.controls.Image;
[Embed(source="/assets/logo.swf")]
[Bindable]
public var imgCls:Class;

private function testPrint():void
{
var testImage:Image = new Image();
testImage.source = "{imgCls}";
this.addChild(testImage);
}
]]>
</mx:Script>

<mx:Button label="Icon Button 1" icon="{imgCls}" click="testPrint()"/>
<mx:Button label="Icon Button 2" icon="{imgCls}"/>
</mx:Application>


Based on an example from the adobe site. The images in the buttons work fine but the image that is attached just appears as a broken image. I've been trying to get this to work since Friday and it seems like it should be very simple!

Hope someone can help.

Thanks

dr_zeus
03-12-2007, 05:41 PM
testImage.source = "{imgCls}";

I don't think this line will work. Binding should be done in MXML. Did you try the following?

testImage.source = imgCls;

Roaders
03-12-2007, 10:22 PM
Thanks for the reply.

In fact I just needed to add an event listener, something like this:


<?xml version="1.0"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">

<mx:Script>
<![CDATA[
import mx.controls.Image;
[Embed(source="/assets/logo.swf")]
[Bindable]
public var imgCls:Class;

private function testPrint():void
{
var testImage:Image = new Image();
testImage.addEventListener(FlexEvent.CREATION_COMP LETE, loadImage);
this.addChild(testImage);
}

private function loadImage(e:Event):void
{
Image(e.currentTarget).load(imgCls);
}

]]>
</mx:Script>

<mx:Button label="Icon Button 1" icon="{imgCls}" click="testPrint()"/>
<mx:Button label="Icon Button 2" icon="{imgCls}"/>
</mx:Application>