Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problem with dynamic load graphics in as3

I make some game, I use I lot of graphics. I make class that have all graphics for example for menu:

public class MenuGFX 
{
    [Embed(source = '../../assets/graphics/menu/background.png')]
    public static const backgroundGFX:Class; 
    [Embed(source='../../assets/graphics/menu/startD.png')] 
    public static const startDGFX:Class; 
    [Embed(source='../../assets/graphics/menu/startO.png')]
    public static const startOGFX:Class; 
    [Embed(source='../../assets/graphics/menu/startU.png')]
    public static const startUGFX:Class; 

}

To load graphic from this I use function:

public class GraphicsLoader
{
    private static var graphicsDictionary:Dictionary = new Dictionary();

    public static function getGraphic(name:String):Bitmap
    {

        var bitmap:Bitmap = graphicsDictionary[name];
        if (bitmap == null)
        {

            var cls:Object = getDefinitionByName("Graphics::" + name) ;
            bitmap = new (cls);
            graphicsDictionary[name] = bitmap;          
        }
        return bitmap;
    }

}

But this working only when I add manual creating some graphics: private var backgroundBitmap:Bitmap = new MenuGFX.backgroundGFX; to loader class:

public class GraphicsLoader
{
    private static var graphicsDictionary:Dictionary = new Dictionary();

    private var backgroundBitmap:Bitmap = new MenuGFX.backgroundGFX;

    public static function getGraphic(name:String):Bitmap
    {

        var bitmap:Bitmap = graphicsDictionary[name];
        if (bitmap == null)
        {

            var cls:Object = getDefinitionByName("Graphics::" + name) ;
            bitmap = new (cls);
            graphicsDictionary[name] = bitmap;          
        }
        return bitmap;
    }

}

I looking for way to avoid making private var someBitmap:Bitmap = new BitmapName; . Do you know how can I deal with this ?

like image 469
piotrek Avatar asked Jun 01 '11 15:06

piotrek


1 Answers

The class MenuGFX will not be compiled into your SWF if the class itself is no where referenced / used within your project source code. It should be enough to call new MenuGFX somewhere in your application (may be in your Main class). The compiler has to know that this class will be used somewhere in your application or it will be ignored.

like image 188
DanielB Avatar answered Oct 18 '22 12:10

DanielB