Search code examples
actionscript-3apache-flexreturn-valuefactoryembedded-resource

AS3/Flex - Returning instantiated object from method (Factory-esque style)


I created a basic class to handle a task I knew I'd want to do a lot over the course of some platformer being programmed in Flixel. The class was written in pure AS3, and meant to hold ongoing facilities to convert embedded files into various data types that took more than one step to do so.

package net.darkglass.conversion
{
import flash.display.BitmapData;
import flash.display.DisplayObject;

public class Embedded
{
    /**
     * Returns a BitmapData loaded with the given embedded image class.
     * 
     * This function does no real error checking.
     * 
     * TODO: Establish basic error checking for function.
     * 
     * @param image An embedded image class object
     * @return BitmapData loaded with contents of Image
     */
    public function imageToBitmapData(image:Class):BitmapData
    {
        // instantiate image as a DisplayObject
        var imageDisplayObject:DisplayObject = new image();

        // create empty BitmapData to use, using image's size for the BitmapData's size
        var imageBitmapData:BitmapData = new BitmapData(imageDisplayObject.width, imageDisplayObject.height);

        // load imageDisplayObject's contents into imageBitmapData
        imageBitmapData.draw(imageDisplayObject);

        // that should be all we need. Return imageBitmapData
        return imageBitmapData;
    }
}
}

I then, in my code, instantiated this object and proceeded in two slightly different ways to try to use it to generate a loaded BitmapData.

The first, which failed (seems to successfully create an empty BitmapData):

// ...
/* Our source image for the test map */
[Embed(source="../asset/graphic/debug/testmap.png")]
public var testmap:Class;
// ...
public var converter:Embedded;
// ...
// test level!
var testmapBD:BitmapData = converter.imageToBitmapData(testmap);

The second, which shows the same results as the first:

// ...
// Only the last few lines change...
// ...
// test level!
var testmapBD:BitmapData = new BitmapData(0, 0);
testmapBD = converter.imageToBitmapData(testmap);
// ...

It appears that AS3 doesn't return the instantiated object? Or, apparently, anything usable? as a one-off, I went ahead and tried to change the BitmapData's dimensions to the correct size from the second case, to no avail:

// ...
// Only the last few lines change...
// ...
// test level!
var testmapBD:BitmapData = new BitmapData(40, 30);
testmapBD = converter.imageToBitmapData(testmap);
// ...

Searching here and around a bit, I was under the impression that primitives are pass-by-value in AS3, and that complex-types are pass-by-reference... And I would assume (however unsafely) that BitmapData is a complex-type.

Is there a way to do this? What am I misunderstanding or missing here?


Solution

  • Far too often, my worst problems in any code seem to be reducable to "did you initialize your objects properly?"

    I cannot manage to credit 32bitkid up above for the answer; however, his answer is the one that is correct given my own code and described problems. Simply initializing the Embedded object converter via converter = new Embedded() fixes the problem with no further change to my code. BitmapData objects can be initialized with the returned data and it behaves as I would suspect it would based on the notes from language spec before.

    However, both Amy Blankenship's answer and Florian Salihovic's answers are valuable as scraps of data and code. Amy Blankenship's answer reveals a little data about the language and its behavior that took me a while to dig out to confirm; bringing such useful information directly to the surface is never bad. Florian's answer is both interesting code (from the point of someone still learning) and useful for specific cases, no doubt.

    The best solution for me here, based on what I actually understand and what I was originally asking is simply "I'm an idiot and need to write a giant sign to hang on the wall opposite my work machine now that says INITIALIZE YOUR OBJECTS PROPERLY". However, that is not to discredit or disparage the usefulness of other solutions offered.

    I do, however, want to turn in my license to hack code together after this. This is the second time on record that my only real problem is not initializing properly.