views:

52

answers:

1

I have a png image on Library that i have declared it via Properties as Background class which extends BitmapData. When i type:
var BMDClass:Class = getDefinitionByName( "Background" ) as Class; i get: variable Background is not defined!! But when i do: trace( getQualifiedClassName( new Background(0,0) ) ); i get: Background !! I can't figure out the cause of the error.

+1  A: 

I believe this is because you need to have a reference to the Background class before you can actually get the definition by name. Simply importing the Background class will not compile the class in to your swf, you need to reference it in some way. Creating an instance of the class is one way, however you can also reference the class after your import.

try something like...

import com.somedomain.Background;
Background;

This should create a reference to you class and ensure it is compiled in to your swf.

Edit to show multiple class usage.

If you have multiple background classes, I would recommend trying to make them adhere to an interface. I would then also create a background factory class that would allow you to create background instances from your configuration file. This also means that you would be able to put all your references to your background classes in the factory class. Here is what the factory could look like.

// let the compiler know we need these background classes
import com.somedomain.backgrounds.*;
DarkBackground;
LightBackground;
ImageBackground;

class BackgroundFactory
{
    public function create(type:String):Background
    {
        var bgClass:Class = getDefinitionByName(type) as Class;
        return new bgClass();
    }
}

Then to get a background instance from your config, you would do something like...

// parse your config file...not sure what format you've got it in.

// instantiate a background factory and create an instance based on the name from your config. 
var bgFactory:BackgroundFactory = new BackgroundFactory();
var bg:Background = bgFactory.create(someStr);

Edit to extend example

package com.somedomain.background
{
    interface Background
    {
        function get img():Bitmap;
    }
}

package com.somedomain.background
{
    class SomeImageBackground extends Sprite implements Background
    {
        protected var _img:Bitmap;

        public function SomeImageBackground():void
        {
            _img = new SomeAssetFromLibrary();
        }

        public function get img():Bitmap
        {
            return _img;
        }
    }
}

Using these external classes would give you a bit more control over where the images come from. You could load them external, embed them using the embed meta data and even load them from the stage.

Chris Gutierrez
I think you are right. I test it as you propose and worked! But now the flexibility of the getDefinitionByName has been blocked as i will have to instantiate the class, but i would like to get its instance by its name so to use a configuration file. Can you propose a solution?
Ponty
I tried to give a more in depth answer for how you could do this with your config file. Let me know if it makes sense.
Chris Gutierrez
hey man thanks very much you give me many ideas! I have a small question again: You write: import com.somedomain.backgrounds.*; but how you put your images in that package? I mean i use either the flash library and convert an image to a user-defined subclass of BitmapData or (just before a little i found) the Embed tag, like [Embed(source="image.png")]public Background1:Class .How do you achieve to gother all your user-defined graphics classes in a package?How do you define an interface if the classes are created just i told you before? Use an interface as a marker? Can you show me?
Ponty
Personally, I would create classes for each background (as display objects) and then inside each class, create an instance of the image that lives in the flash library. You can then define functionality in each of those classes. Things like resizing etc... I've edited the answer again to include an example.
Chris Gutierrez
what can i say thanks a lot. I will study the examples and i will try to work out the solution to my problem. When you write to your first examples after import statement: DarkBackground; etc, you have declared them in the library as DarkBackground or have you used Embed metadata (or it doesn't matter)? What I'm trying to do: I have a graphics folder with background, logo, and other subfolders full of png graphics.I want to make an application that can produce a different SWF when i change the png files.And i have to load the pngs to an array as a BitmapData or a subclass.Plz make acomm onthis
Ponty
Many thanks for your time. Very helpfull answers.Really appreciated!!!
Ponty