Apache flex 在Actionscript 3.0中动态创建变量?

Apache flex 在Actionscript 3.0中动态创建变量?,apache-flex,actionscript-3,dynamic,variables,Apache Flex,Actionscript 3,Dynamic,Variables,我正在加载几个声音文件,并希望对每个加载进行错误检查。但是,我希望它们都使用相同的完整/错误处理函数,而不是使用各自的完整/错误函数来编程 成功加载的声音应创建一个新的声音通道变量,而未成功加载的声音将生成一个带有未能加载声音名称的简单跟踪。然而,为了做到这一点,我需要动态地创建变量,我还没有弄清楚如何创建变量 以下是我完整和错误函数的代码: function soundLoadedOK(e:Event):void { //EX: Sound named "explosion" will c

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

成功加载的声音应创建一个新的声音通道变量,而未成功加载的声音将生成一个带有未能加载声音名称的简单跟踪。然而,为了做到这一点,我需要动态地创建变量,我还没有弄清楚如何创建变量

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

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);
 }
-=-=-=-=-=-=-=-=- 更新(关于:viatropos) -=-=-=-=-=-=-=-=-

找不到错误

TypeError:错误#1009:无法访问空对象引用的属性或方法。在第12课中,佛罗里达州开始::Main Timeline/loadSounds()在第12课中,佛罗里达州开始::Main Timeline/frame1():

//声音
var soundByName:Object={};
var channelByName:Object={};
var soundName:String;
var channelName:字符串;
loadSounds();
函数loadSounds():void
{
var文件:Array=[“robotArm.mp3”,“click.mp3”];
变量i:int=0;
var n:int=files.length;
对于(i;i
您可以通过以下几种方式完成此操作:

  • 将您的声道存储在阵列中。如果你关心订单,或者你不关心他们的名字,那就好了
  • 对象中的任何名称存储声音通道
    。如果你想很容易地得到名字,那就好了。注意,
    Object
    类只能存储作为字符串的键(
    {key:value}
    Object[key]=value
    )。如果您需要对象作为键,请使用
    flash.utils.Dictionary
    ,它是一个美化的散列
  • 下面是一个示例,演示如何使用数组和对象

    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);
    }
    
    var通道:数组=[];
    //而不是创建大量的属性,比如
    //propA propB propC
    //只需创建一个属性并让它保留这些值
    var channelsByName:Object={};
    函数loadSounds():void
    {
    var文件:Array=[“soundA.mp3”、“soundB.mp3”、“soundC.mp3”];
    声音:声音;
    var soundChannel:soundChannel;
    变量i:int=0;
    var n:int=files.length;
    对于(i;i
    让我知道这是否可行。

    //加载声音
    
    //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();
    
    var soundDictionary:Dictionary=newdictionary(); var soundByName:Object=新对象(); var channelByName:Object=新对象(); loadSounds(); 函数loadSounds():void { var文件:Array=[“robotArm.mp3”,“click.mp3”];//等等。 for(var i:int=0;i
    您可以将新频道添加到数组(channels.push(new SoundChannel())或字典中。例如,让声音频道易于通过名称引用会有所帮助。此外,您可能还想看看Matt Przybylski博客()上的SoundManager.as类.当然。但就我所见,我的问题是,虽然我可以迭代一系列声音,逐个发送它们以检查完整/错误,但如果每个声音加载成功,我需要为每个声音创建单独的声道变量。谢谢。我明白你的指示。尽管我仍然有一个无法解决的错误。为了播放声音,我还需要创建一个soundByName:Object和一个channelByName:Object来停止声音。我已经用我的新代码更新了我的原始帖子。我仍然不明白更新的代码有什么问题。我更新了我的代码和你的代码,我们只需要实例化hash:channelsByName:Object={}.新问题:ReferenceError:错误#1069:在flash.media.Sound上找不到属性名称,并且没有默认值。在第12课\u start\u fla::Main
    //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();