1

我想把所有东西都做成一个Sprite,因为我听说这是个好主意,直​​到它不是专业动画。我是这样做的:

//资产.as

[Embed(source = "../lib/Textures/Game/GameBackground.jpg")] public static const GameBackground:Class;

然后当我想使用它时我有课:

//游戏.as

private var pic:Bitmap = new Assets.GameBackground();
private var DATA:Vector.<BitmapData> = new Vector.<BitmapData>([pic.bitmapData]);
private var backgroundImg:Bitmap = new Bitmap(new BitmapData(1, 1));    

public function Game(){
    addChild(backgroundImg);
    backgroundImg.bitmapData = DATA[0];
}

我知道当我只有一个位图来使用矢量时它是没用的,但稍后会有带有两个位图的精灵表或按钮。更重要的是,我想将这些向量存储在 Asstets.as 中,即使尝试在我希望它使用的类中创建它们也有问题,原因:

  1. 尝试:这种方式:data:Vector.<BitmapData> = new Vector.<BitmapData>([pic]);
    接收错误:索引 0 超出范围 0。

  2. 尝试:这种方式:data:Vector.<BitmapData> = new Vector.<BitmapData>[pic];
    接收:在非构造函数上尝试实例化。我认为这很明显。

当我只使用它pic:Bitmap并且backgroundImg.bitmapData = pic.bitmapData它可以工作时,但我不希望这样。当一个精灵有更多位图时需要使用矢量。甚至是一个世界。更重要的是,想在 Assets.as 类中创建这些向量。

4

2 回答 2

1

当您创建一个新的 Vector 实例时,它接受的唯一参数(可选)是 Vector 的长度(您将拥有多少个元素)。

new Vector:<BitmapData>(5)

这将告诉向量它将有 5 个元素。与数组不同,您不能在构造函数中传入元素。如果您省略参数,它会使长度动态化,因此它可以根据需要增长/收缩。

您需要执行以下操作:

data:Vector.<BitmapData> = new Vector:<BitmapData>();
data.push(pic.bitmapData);

更多关于创建向量 http://help.adobe.com/en_US/as3/dev/WS5b3ccc516d4fbf351e63e3d118a9b90204-7ee5.html#WSB1F41227-C612-4f33-A00E-CE84C1913E1C

于 2012-09-06T16:42:07.577 回答
0

你的想法很不错=O

我为一些全局图像做了它并且效果很好:

[Embed(source="greenled.png")] private static const greenled:Class;
[Embed(source="yellowled.png")] private static const yellowled:Class;
[Embed(source="redled.png")] private static const redled:Class;
public static const SIGN:Vector.<Class> = Vector.<Class>([greenled,yellowled,redled]);
于 2012-10-10T23:17:58.987 回答