Comments on: Create a pseudo-class from a runtime-loaded image in AS3 https://joshblog.net/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/ Archive of older blog posts written by Josh Tynjala about Flash, Flex, and ActionScript Wed, 26 Jun 2013 02:52:46 +0000 hourly 1 https://wordpress.org/?v=4.9.9 By: Don https://joshblog.net/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-35103 Mon, 05 Mar 2012 06:21:46 +0000 http://www.zeuslabs.us/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-35103 Thanks Josh. That works.

]]>
By: Josh Tynjala https://joshblog.net/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-35068 Sun, 04 Mar 2012 23:23:38 +0000 http://www.zeuslabs.us/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-35068 Don, you should probably wait until the image is loaded before you resize it.

]]>
By: Don https://joshblog.net/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-34976 Sun, 04 Mar 2012 08:26:01 +0000 http://www.zeuslabs.us/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-34976 Hi Josh, how do I resize the loaded bitmap from your loader function? I tried resize the image to half the size (see below), but nothing is displayed. Thanks in advance.

//loaded image is 100x100, want to resize it to 50x50
var ImageType:Function = LoaderUtil.createAutoLoader(o.url);
var image1:Loader = new ImageType();
image1.width = 50;
image1.scaleY = image1.scaleX;
addChild(image1);
]]>
By: Josh Tynjala https://joshblog.net/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-27066 Tue, 03 Jan 2012 05:09:47 +0000 http://www.zeuslabs.us/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-27066 Don, it returns a function that you can use with the new keyword, like a class.

var ImageType:Function = LoaderUtil.createAutoLoader(o.url);
var image1:Loader = new ImageType();
addChild(image1);
]]>
By: Don https://joshblog.net/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-27040 Tue, 03 Jan 2012 03:00:04 +0000 http://www.zeuslabs.us/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-27040 Hi Josh.
I’m pretty new to AS3. How do you use the “createAutoLoader” class function? I did the following but getting error during execution. Any help is appreciated.

var image1 = LoaderUtil.createAutoLoader(o.url);
addChild(image1);

TypeError: Error #1034: Type Coercion failed: cannot convert Function to flash.display.DisplayObject.

]]>
By: Metal Hurlant https://joshblog.net/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-1495 Wed, 21 Nov 2007 02:07:52 +0000 http://www.zeuslabs.us/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-1495 Prototype isn’t strictly needed here. A function created within a function is a closure, so it keeps a snapshot of the outer functions’ locale variables in its scope.

The body of createAutoLoader() can simply be:

return function():Loader {
    var loader:Loader = new Loader();
    loader.load(new URLRequest(url), context);
    return loader;
};

One behavioral difference between this and an embedded asset is the dreaded one-frame minimum wait for the remotely loaded image, even when the image is already in the browser cache.
There are workarounds for it. Ely Greenfield has a Flex SuperImage component that gets the general idea right.
To deal with multiple loads of the same asset, you’d want to cache the BitmapData object, and associates that with the url. Further attempts to instanciate the AutoLoader would just create a new Bitmap with that BitmapData.

]]>
By: Scott https://joshblog.net/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-1494 Mon, 29 Oct 2007 19:38:03 +0000 http://www.zeuslabs.us/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-1494 There’s another solution, but it’s really hacky. You can create a stand-alone SWF that has your MyImageClass as a symbol in it (make sure to save the swf as uncompressed) and load it into your SWF as a bytearray. Save a copy of this bytearray once it’s loaded.

When you need to create a new class, make a copy of the bytearray and patch the data in the bytearray containing the string to the string you’re interested in. This clearly will require some hacking around with the SWF file format to make this work. Finally, load from the bytearray and use getDefinition to get the class object.

This is clearly way overkill for the case at hand, though. But it does illuminate thte possibility of generating classes at run-time. It’s just very, very difficult.

Too bad actionscript doesn’t have eval(). 🙂

]]>
By: Josh Tynjala https://joshblog.net/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-1493 Mon, 22 Oct 2007 15:51:50 +0000 http://www.zeuslabs.us/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-1493 Ian, I have not found a better solution.

]]>
By: Ian Thomas https://joshblog.net/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-1492 Mon, 22 Oct 2007 15:14:24 +0000 http://www.zeuslabs.us/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-1492 Hi Josh,
Sounds like I’m currently jumping through exactly the same hoops as you; having real trouble generating a Class object uniquely tied to a BitmapAsset with some associated bitmap Data (I’m trying to do something like you describe with the Flex .icon property).

It’s maddening. Did you get any further than you describe here?

Ian

]]>
By: Josh Tynjala https://joshblog.net/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-1491 Thu, 04 Oct 2007 06:33:32 +0000 http://www.zeuslabs.us/2007/09/28/actionscript-3-bitmap-image-classes-with-functions-and-loaders/#comment-1491 Torbjørn, I wouldn’t be golden with your suggested method. What if I have two images to load? The static var only supports a single image unless I take special care in timing when the class may be used. I’ll need to change the URL to load the second image, but what if the component needs to use the first URL again? It will have been lost.

Trust me, I tried many possible approaches over a period of a few days, including what you’ve suggested.

]]>