Jump to content
  • Advertisement
Sign in to follow this  
d000hg

[Flex]Loading an image programatically

This topic is 3992 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
Advertisement
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
i think that is what you're looking for:
http://livedocs.adobe.com/flex/2/langref/flash/display/Loader.html

Share this post


Link to post
Share on other sites
I haven't got it working yet, but apparently mx.controls.Image does what I need:

private var image:Image = new Image();
image.load("images/image001.png");


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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!