as3中动态加载图形的问题

时间:2011-06-01 15:45:56

标签: actionscript-3

我做了一些游戏,我使用了很多图形。 我制作了具有所有图形的类,例如菜单:

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; 

}

要从中加载图形我使用函数:

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;
    }

}

但这只适用于我添加手动创建一些图形:private var backgroundBitmap:Bitmap = new MenuGFX.backgroundGFX;到装载程序类:

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;
    }

}

我正在寻找避免制作private var someBitmap:Bitmap = new BitmapName;的方法。你知道我怎么处理这个?

1 个答案:

答案 0 :(得分:3)

如果类本身不是项目源代码中引用/使用的类,则不会将类MenuGFX编译到SWF中。在应用程序的某个地方(可能在您的Main类中)调用new MenuGFX就足够了。编译器必须知道该类将在您的应用程序中的某个地方使用,否则将被忽略。