Search code examples
actionscript-3flashdevelop

Assets class calls not working


I'm making a class that embeds all the assets I need, icons, fonts, images, etc; I took it from Adrian's answer here.

This is pretty much the structure it has:

public class Assets 
{
    [Embed(source = "../assets/images/imageBG.png")]
    private static var imageBG:Class;

    [Bindable]
    private var imageName:Class;//There wasn't a ";" here, no error but I added it just in case

    public static function setImage(newImageName:Class):void
    {
        imageName = newImageName:Class;
    }
}

And I'm supposed to use something like this from a different class on the same package to call the asset I need (although it should work in any other class)

    image = new Assets.setImage(imageBG) as Bitmap;

As it is, it trows "Label must be a simple identifier."

if I change

imageName = newImageName:Class;

to

imageName = newImageName; //The "Class" part was pointed as the error

it trows this "Access of undefined property imageName."

when is called on the function and this from wherever it's called

"Method cannot be used as a constructor."

pointed right after the "Assets." part.

I'm obviously doing something wrong but I don't know what it is, if anyone can help please.

Also, is OK if I ask how can I change things so that I can call the asset passing it's name as a string? or should I make another question?

Thanks in advance.


Solution

  • imageName is not static and so doesn't exist in static scope.

    The answer you refer to was accepted despite having errors. Also it was for Flex projects only. This is the same but corrected:

    public class Assets 
    {
        static private var assetref:Object = {};
    
        [Embed(source = "../assets/images/imageBG.png")]
        private static var imageBG:Class; 
    
        assetref["imageBG"] = imageBG;   
    
        public static function getImage(newImageName:String):Bitmap
        {
            if(assetref[newImageName])
            {
                return new assetref[newImageName]();
            }
            return null;
        }
    }
    

    just pass the name of the asset you need as a String and if it exist you'll get back a bitmap ready to display.