来自Flash目录的随机图像

时间:2013-09-10 04:58:51

标签: flash actionscript

我想从我的目录中随机拍摄图像并在Flash中显示,但我不知道该怎么做 请帮帮我。

1 个答案:

答案 0 :(得分:0)

不确定AS3的Math.random是否接受uint(unsigned int),如果没有将所有uint切换为Number,请参阅Math.Random上的文档,但这将引导您朝着正确的方向前进。

[Embed(source='/assets/images/1.jpg')]
    private var EmbeddedImage1:Class;
[Embed(source='/assets/images/2.jpg')]
    private var EmbeddedImage2:Class;
[Embed(source='/assets/images/3.jpg')]
    private var EmbeddedImage3:Class;
//and so on...

  // CONSTANTS
public const MIN_RANDOM:uint= 1;
public const MAX_RANDOM:uint= 10;

public function Main():void {
    var random_num:unit = randomIntBetween(MIN_RANDOM, MAX_RANDOM);

    if (stage) {
        this.init();
    }

    switch (random_num) {
        case 1:
            stage.AddChild(new EmbeddedImage1:Class);
        break;
        case 2:
            stage.AddChild(new EmbeddedImage2:Class);
        break;
        //and so on...
    }
}

private function randomIntBetween(min:uint, max:uint):uint {
    return Math.round(Math.random() * (max - min) + min);
}