Sign in to follow this  

[Flex]Loading an image programatically

This topic is 3742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've seen an exampl of how to create objects from images in Flex where the image is embedded as a special class. But I wish to load an image from a file - you can do it in MXML using an mx:Image tag but how would I do this programmatically. I can't find an example in the help, every class which looks useful seems to assume you already have your loaded image. Thanks.

Share this post


Link to post
Share on other sites
you declare it like:

[Embed(source="texture.jpg")]
private var Texture:Class;

and then instantiate it:

var bmp:Bitmap=new Texture();

Share this post


Link to post
Share on other sites
Doesn't [Embed] cause the iamge to be compiled into the .SWF? When you do it through .mxml it actually loads the image from the source folder... I'll try it and see.

But what if I have 10 file like 1.jpg, 2.jpg,... 10.jpg? Can I do this in a loop - is Embed a special command or just a normal langauge feature?

I was expecting something like var x:Image = new Image("pictures/4.jpg") - does such a way exist?

Share this post


Link to post
Share on other sites
So, I looked online at examples and they just don't work. But the embedded version does. Here is a simple test class:
package src
{
import mx.controls.*;
import mx.core.UIComponent;
import flash.net.URLRequest;
import flash.display.*;

public class D000hg extends UIComponent
{
[Embed(source="../art/1.png")]
private var IMG:Class;


public function init():void
{
var sprite:DisplayObject = new IMG();
sprite.x =35;
sprite.y = 200;
sprite.visible = true;
addChild(sprite);

var sprite2:SWFLoader = new SWFLoader();
var url:String = "art/2.png";
var urlReq:URLRequest = new URLRequest(url);
sprite2.load(urlReq);
sprite2.x =200;
sprite2.y = 200;
sprite2.visible = true;
this.addChild(sprite2);

var rect:Shape = new Shape();
rect.graphics.beginFill(0xFFFFFF);
rect.graphics.drawRect(300, 200, 100, 100);
rect.graphics.endFill();
addChild(rect);
var ldr:Loader = new Loader();
ldr.mask = rect;
var url2:String = "art/3.png";
var urlReq2:URLRequest = new URLRequest(url2);
ldr.load(urlReq2);
addChild(ldr);
}
}
}

The first one works; the other two don't. The difference in path (the ../) is not the cause, I've tried (it's due to the embedding).

Having to embed all 40 images doesn't sound fun to me. What's wrong?

Share this post


Link to post
Share on other sites

This topic is 3742 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this