views:

172

answers:

1

i'm attempting to instantiate a bunch of sounds by creating a string array containing each sound's filepath (or name).

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

function loadSounds():void
    {
    var files:Array = new Array("sound1.mp3", "sound2.mp3"); //etc.
    for (var i:int = 0; i < files.length; 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(e:Event):void
    {
    channelName = e.currentTarget.name;
    channelByName.channelName = new SoundChannel();
    }

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

then called like this:

//Stop a sound
channelByName["sound1.mp3"].stop();

//Play a sound
channelByName["sound2.mp3"] = soundByName["sound2.mp3"].play();

my current code contains an error from the sound_completeHandler() function stating that the 'name' property wasn't found. i can't figure out how to add this name property, or how else to reference the e.currentTarget.

+2  A: 

Your code is wrong in 3 parts:

  • soundByName is an Object and you are doing a soundByName.soundName=new Sound() => you are creating a field named soundName within soundByName. Use soundByName[soundName]=new Sound(); which mean create a field with the name taken from the variable coundName.

  • You are doing the same with channelByName use channelByName[channelName]=value;

  • Then you want to associate a soundChannel from your name, it can't work Sound object have no such field. Use a dictionary where you will associating the sound with the name.

    var nameBySound:Dictionary = new Dictionary();
    var soundByName:Object = {};
    var channelByName:Object = {};
    loadSounds();
    
    
    function loadSounds():void {
      var files:Array = ["sound1.mp3", "sound2.mp3"]; //etc.
      for (var i:int = 0; i < files.length; i++) {
       var soundName:String = files[i];
       var sound:Sound=new Sound(); 
       nameBySound[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=nameBySound[e.currentTarget];                      
     channelByName[soundName] = new SoundChannel();                          
    }                                                                        
    
    
    function sound_ioErrorHandler(e:IOErrorEvent):void {
     trace("Failed To Load Sound:" + nameBySound[e.currentTarget]);
    }
    
Patrick
thanks. this works well. i've been meaning to read about the dictionary class. in your opinion, do you think this approach of mine is the good way for loading / calling many sounds?
TheDarkInI1978
Well you can for example use only one dictionary to put all the infos into (name, sound, soundchannel). It depends also of your usage of the code.
Patrick
Thanks for this example Patrick.
daidai
@daidai You're welcome
Patrick