AS3 嵌入图像类,然后将这些图像放入另一个类? [英] AS3 Embed images class and then get these images into another class?

查看:17
本文介绍了AS3 嵌入图像类,然后将这些图像放入另一个类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例如,现在我有一个名为Balls.as"的类.在这里,我加载了 10 个不同的球图像.你知道,像这样:

For example, right now I have a class called "Balls.as". Here I load 10 different balls-images. You know, like this:

[Embed(source = "/ball1.png")]
[Embed(source = "/ball2.png")]

问题是,如果我生成 5 个球,这些球图像会被嵌入 5 * 5 次,对吗?如我错了请纠正我!所以我不能有一个 ballimageloading 类吗?加载这些图像一次,然后在 Balls 中.因为我可以加载我现在想要的任何球?

The problem is that if I spawn 5 balls, these balls images gonna get embeded 5 * 5 times right? Correct me if I'm wrong! So I though, can't I have a ballimageloading class? That loads these images once, and then in Balls.as I can load whatever ball i want for the moment?

推荐答案

您不需要加载"它们,它们是嵌入的.你只需要实例化图像.有一个管理共享资源的类是个好主意,例如:

You won't need to "load" them, they are embedded. You just have to instantiate the images. It's a good idea to have one class that manage shared ressources, as such:

public class TextureAssets 
{
    [Embed(source = "../lib/ball1.png")]
    private static var Ball1:Class;

    [Embed(source = "../lib/ball2.png")]
    private static var Ball2:Class;

    public static var ball1Texture:BitmapData;
    public static var ball2Texture:BitmapData;

    public static function init():void
    {
        ball1Texture = (new Ball1() as Bitmap).bitmapData;
        ball2Texture = (new Ball2() as Bitmap).bitmapData;
    }

然后你会调用 TextureAssets.init() 一次(例如在 Main.as 中)当您需要位图数据时:使用 new Bitmap(TextureAssets.ball1Texture)
这样您的程序只需要使用一个位图数据所需的内存,而不是使用许多最终相同的内存.
如果您需要在保留原始位图数据的同时对位图数据执行操作,您可以使用:

Then you would call TextureAssets.init() once (in the Main.as for example) and when you need the bitmapData: use new Bitmap(TextureAssets.ball1Texture)
This way your program needs uses only memory required for one bitmapData instead of having many which ends up being the same.
If you need to perform operations on a bitmapData while keeping the original you can use:

var modified:bitmapData = TextureAssets.ballTexture.clone();


此外,如果您要从一个类中实例化所有球图像,最好放弃静态访问,而是在构造函数中初始化 bitmapDatas,创建一个新的 TextureAssets() 并通过变量调用纹理
(静态字段访问比直接 (.) 访问慢:http://jacksondunstan.com/articles/1690)

这篇关于AS3 嵌入图像类,然后将这些图像放入另一个类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆