View Full Version : Help with Loading External Images with AS3
Ibanez271
August 6th, 2008, 12:33 PM
I just want to be able to load an image to the stage through actionscript.
Here is the code I have that I thought would work
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, imageLoaded);
loader.load(new URLRequest("bluelock.gif"));
function imageLoaded(event:Event):void
{
var image:Bitmap = new Bitmap(event.target.content.bitmapData);
addChild(image);
}
Alright so the code does find the image and runs my imageLoaded function. Then on the stage it seems to be loading something on to the stage, but I cannot see anything? There is no image.
mattrock23
August 6th, 2008, 01:55 PM
var image = Bitmap(event.target.loader.content);
Ibanez271
August 6th, 2008, 02:05 PM
Gives me a TypeError: Type Coercion failed: cannot convert flash.display to flash.display.BitmapData
mattrock23
August 6th, 2008, 02:18 PM
Well, that's weird, I copy/pasted that line from my image gallery script. I'll see if I can figure out why.
Ibanez271
August 6th, 2008, 02:29 PM
When I trace some of the attributes of the image variable it seems to have everything correct. For example if I trace(image.width) or trace(image.height) it has the the correct width and height according to my image loaded. I am basically just unsure why the actual image doesn't load or show up onto the stage?
mattrock23
August 6th, 2008, 02:38 PM
A bitmapdata object is not a displayobject. So even though it can tell you the height and width of the loaded content it cannot be displayed.
Did you take out the "new" keyword? The line in the imgLoaded function should read just like I posted earlier. (You can add a datatype to the image var if you want)
Ibanez271
August 6th, 2008, 02:45 PM
Alright I put the line in and it does not give an error but it also does not display anything on the stage. The stage is just blank when the movie is ran. Thanks for all the help also.
mattrock23
August 6th, 2008, 02:48 PM
What does you code look like now?
Ibanez271
August 6th, 2008, 02:53 PM
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, imageLoaded);
loader.load(new URLRequest("bluelock.gif"));
function imageLoaded(event:Event):void
{
var image = Bitmap(event.target.loader.content);
addChild(image);
}
It still brings nothing to the screen?
spectator
August 6th, 2008, 02:59 PM
event.target.data ?
Ibanez271
August 6th, 2008, 03:01 PM
event.target.data is a no go.
mattrock23
August 6th, 2008, 03:06 PM
You could try tracing the x and y of image to see if it actually is on the stage and where.
Alex Lexcuk
August 6th, 2008, 03:14 PM
var loader:Loader = new Loader();
loader.load(new URLRequest("http://www.dnadillo.dn.ua/images/baltimor.jpg"));
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, imageLoaded);
function imageLoaded(event:Event):void
{
addChild(loader.content);
}
http://dnadillo.dn.ua/fla/XML/img-xml-img.swf
http://dnadillo.dn.ua/fla/XML/img-xml-img.zip
http://dnadillo.dn.ua/fla/XML/img_xml.xml
Ibanez271
August 6th, 2008, 03:24 PM
Alex when I tried your code nothing was shown on the screen. It finds the image and runs the event but nothing is displayed. Mattrock23 I traced the x and y and they do show up as 0 , 0 because that is the default. This is just weird, I figured adding an image would work how it is supposed to.
skial
August 6th, 2008, 04:35 PM
Hi, try this.
function imageLoaded(event:Event):void
{
var _w:int = event.target.content.width;
var _h:int = event.target.content.height;
var _bit:BitmapData = new BitmapData(_w, _h);
_bit.draw(event.target.content)
var image:Bitmap = new Bitmap(_bit);
addChild(image);
}
For me this does show an error, now and then, but there must be a better way than this.
Cheers:beer:
Skial
Ibanez271
August 7th, 2008, 12:20 PM
Skial,
I put all this code in and I got no error message when run. But my stage is still empty with no image pictured inside?
skial
August 7th, 2008, 12:32 PM
Hi, I just created a quick version of the code you gave at the beginning of the thread, and what I posted, and it worked fine for me, I have included the files, all I changed was the image name. Code also below:
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, imageLoaded);
loader.load(new URLRequest("taosign-waiting.png"));
function imageLoaded(event:Event):void
{
var _w:int = event.target.content.width;
var _h:int = event.target.content.height;
var _bit:BitmapData = new BitmapData(_w, _h);
_bit.draw(event.target.content)
var image:Bitmap = new Bitmap(_bit);
addChild(image);
}
Hope it helps.
Cheers:beer:
Skial
Ibanez271
August 7th, 2008, 12:36 PM
I just hacked my way to it :). I placed an image inside a movie clip. Converted the movieclip to a class. Then used the code
var class_obj:Object = getDefinitionByName("MovieClipName");
addChild(new class_obj());
skial
August 7th, 2008, 01:03 PM
Hi, I thought you wanted to get the loaded image to be used as a bitmapdata object, so then you could put some effects on it. But am I right that all you wanted to do was add the loaded object to the stage? If so, then code below works.
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, imageLoaded);
loader.load(new URLRequest("taosign-waiting.png"));
function imageLoaded(event:Event):void
{
addChild(event.target.content);
}
If not, then im confused...
Cheers:beer:
Skial
Ibanez271
August 7th, 2008, 02:33 PM
This is true all I wanted to do was add the image to the stage. When I tried that exact code I cannot actually see any image on stage when the movie is run. When I use a movieclip from the library with the image I want already inside then I got it. I am definitely confused why the first code does not work for me, because there would be many cases where I would want to load images from my hard drive or web server?
Alex Lexcuk
August 7th, 2008, 03:02 PM
try this.
var context:LoaderContext = new LoaderContext();
context.checkPolicyFile = true;
var loader:Loader = new Loader();
loader.load(new URLRequest("http://www.dnadillo.dn.ua/images/baltimor.jpg"),context);
loader.contentLoaderInfo.addEventListener(Event.CO MPLETE, imageLoaded);
function imageLoaded(event:Event):void
{
addChild(loader.content);
}
with external site works
http://murmadillo.tut.su/fla/Load_one_image.swf
since have
http://dnadillo.dn.ua/crossdomain.xml
MadTurki
December 18th, 2008, 11:52 AM
Sorry - this thread is a bit old... Based on Alex's code above, how can I now adjust my .x and .y for my newly created child "addChild(loader.content)"?
Thanks in advance...
skial
December 22nd, 2008, 02:22 PM
If you havnt already figured out the answer, just add the loader.content to a Sprite or MovieClip, and then move that Sprite or MovieClip with the .x / .y. Using addChild(loader.content) will add the loader content to the stage display list, so adding it to a Sprite or MovieClip allows you to edit the position.
Cheers :beer:
Skial
MadTurki
January 13th, 2009, 12:01 PM
Awesome! I'm not really sure how I'd do this :P
MadTurki
January 13th, 2009, 12:12 PM
Nevermind! I got it by using this tutorial: http://www.flashandmath.com/intermediate/children/index.html
skial
January 13th, 2009, 12:20 PM
var l:Loader = new Loader();
var h:Sprite = new Sprite();
init();
function init():void {
l.contentLoaderInfo.addEventListener(Event.COMPLET E, onComplete);
l.load(new URLRequest("file/path/here.png"));
}
function onComplete(e:Event):void {
this.addChild(h);
h.addChild(e.target.content);
h.x = 10;
h.y = 20;
}Hopefully this doesnt have any errors, and should run on the timeline.
Cheers:beer:
Skial
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.