在Actionscript 3.0中动态创建变量?

时间:2010-03-02 11:57:21

标签: flex actionscript-3 dynamic variables

我正在加载几个声音文件,并希望错误检查每个加载。但是,相反使用自己的完整/错误函数对每个函数进行编程,我希望它们都使用相同的完整/错误处理函数。

成功加载的声音应创建新的声道变量,而未成功加载的声音将产生一个简单的轨迹,其中包含无法加载的声音名称。但是,为了做到这一点,我需要动态创建变量,我还没有想出怎么做。

这是我的完整和错误函数的代码:

function soundLoadedOK(e:Event):void
 {
 //EX: Sound named "explosion" will create Sound Channel named "explosionChannel"
 var String(e.currentTarget.name + "Channel"):SoundChannel = new SoundChannel();
 }

function soundLoadFailed(e:IOErrorEvent):void
 {
 trace("Failed To Load Sound:" + e.currentTarget.name);
 }

- = - = - = - = - = - = - = - = - 更新(RE:viatropos) - = - = - = - = - = - = - = - = -

无法找到错误。

  

TypeError:错误#1009:无法访问空对象引用的属性或方法。在lesson12_start_fla :: MainTimeline / loadSounds()在lesson12_start_fla :: MainTimeline / frame1():

//Sounds
var soundByName:Object = {};
var channelByName:Object = {};
var soundName:String;
var channelName:String;
loadSounds();

function loadSounds():void
{
    var files:Array = ["robotArm.mp3", "click.mp3"];
    var i:int = 0;
    var n:int = files.length;
    for (i; i < n; i++)
    {
        soundName = files[i];
        soundByName[soundName] = new Sound();
        soundByName[soundName].addEventListener(Event.COMPLETE, sound_completeHandler);
        soundByName[soundName].addEventListener(IOErrorEvent.IO_ERROR, sound_ioErrorHandler);
        soundByName[soundName].load(new URLRequest(soundName));
    }
}

function sound_completeHandler(event:Event):void
{
    channelName = event.currentTarget.id3.songName;
    channelByName[channelName] = new SoundChannel();
}

function sound_ioErrorHandler(event:IOErrorEvent):void
{
    trace("Failed To Load Sound:" + event.currentTarget.name);
}

2 个答案:

答案 0 :(得分:3)

您可以通过以下几种方式实现这一目标:

  1. 将SoundChannel存储在数组中。如果您关心订单,或者您不关心按名称获取订单,那就太好了。
  2. Object中以任何名称存储SoundChannel。如果你想轻松获得名字,那就太好了。请注意,Object类只能存储字符串({key:value}object[key] = value)。如果您需要将对象作为键,请使用flash.utils.Dictionary,这是一个美化的哈希。
  3. 这是一个演示使用数组与对象的示例。

    var channels:Array = [];
    // instead of creating a ton of properties like
    // propA propB propC
    // just create one property and have it keep those values
    var channelsByName:Object = {};
    
    function loadSounds():void
    {
        var files:Array = ["soundA.mp3", "soundB.mp3", "soundC.mp3"];
        var sound:Sound;
        var soundChannel:SoundChannel;
        var i:int = 0;
        var n:int = files.length;
        for (i; i < n; i++)
        {
            sound = new Sound();
            sound.addEventListener(Event.COMPLETE, sound_completeHandler);
            sound.addEventListener(IOErrorEvent.IO_ERROR, sound_ioErrorHandler);
            sound.load(files[i]);
        }
    }
    
    function sound_completeHandler(event:Event):void
    {
        // option A
        var channelName:String = event.currentTarget.id3.songName;
        // if you want to be able to get them by name
        channelsByName[channelName] = new SoundChannel();
    
        // optionB
        // if you just need to keep track of all of them,
        // and don't care about the name specifically
        channels.push(new SoundChannel())
    }
    
    function sound_ioErrorHandler(event:IOErrorEvent):void
    {
        trace("Failed To Load Sound:" + event.currentTarget.name);
    }
    

    如果有问题,请告诉我。

答案 1 :(得分:0)

//Load Sounds
var soundDictionary:Dictionary = new Dictionary();
var soundByName:Object = new Object();
var channelByName:Object = new Object();

loadSounds();

function loadSounds():void
    {
    var files:Array = ["robotArm.mp3", "click.mp3"]; //etc.
    for (var i:int = 0; i < files.length; i++)
        {
        var soundName:String = files[i];
        var sound:Sound=new Sound(); 
        soundDictionary[sound] = soundName;
        soundByName[soundName] = sound;
        sound.addEventListener(Event.COMPLETE, sound_completeHandler);
        sound.addEventListener(IOErrorEvent.IO_ERROR, sound_ioErrorHandler); 
        sound.load(new URLRequest(soundName));
        }
    }                                                                        

function sound_completeHandler(e:Event):void
    {
    var soundName:String=soundDictionary[e.currentTarget];                      
    channelByName[soundName] = new SoundChannel();                          
    }

function sound_ioErrorHandler(e:IOErrorEvent):void
    {
    trace("Failed To Load Sound:" + soundDictionary[e.currentTarget]);
    }

//Play Sound
channelByName["robotArm.mp3"] = soundByName["robotArm.mp3"].play();

//Stop Sound
channelByName["robotArm.mp3"].stop();