ActionScript为加载/呼叫声从字符串数组数组、字符串、加载、ActionScript

由网友(君心今何在°)分享简介:我试图创建一个字符串数组,包含每个声音的文件路径(或名称),以实例化一串声音。i'm attempting to instantiate a bunch of sounds by creating a string array containing each sound's filepath (or name).v...

我试图创建一个字符串数组,包含每个声音的文件路径(或名称),以实例化一串声音。

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();

我目前的code包含了从sound_completeHandler()函数,说明未找到名称属性的错误。我无法弄清楚如何添加这个名字属性,或者怎么回事引用e.currentTarget。

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.

推荐答案

您code是错误的3个部分:

Your code is wrong in 3 parts:

soundByName是一个对象,你在做一个 soundByName.soundName =新的声音() =>您正在创建一个名为内soundByName soundName领域。 使用 soundByName [soundName] =新的声音(); 这意味着从变量coundName采取的名称创建一个字段

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.

您正在做同样的与channelByName使用 channelByName [channelName] =值;

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

然后你要在的SoundChannel 从你的名字,它不能正常工作声音对象关联没有这样的字段。使用一本字典,你将有关联的名字的声音。

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]);
}

阅读全文

相关推荐

最新文章