Actionscript从String Array加载/调用声音 [英] Actionscript Loading/Calling Sounds From String Array
问题描述
我试图通过创建一个包含每个声音的文件路径(或名称)的字符串数组来实例化一堆声音。
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);
}
然后调用如下:
//Stop a sound
channelByName["sound1.mp3"].stop();
//Play a sound
channelByName["sound2.mp3"] = soundByName["sound2.mp3"].play();
我当前的代码包含一个来自sound_completeHandler()函数的错误,指出'name'属性是'发现。我不知道如何添加这个name属性,或者如何引用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.
推荐答案
你的代码在3部分错误:
Your code is wrong in 3 parts:
-
soundByName是一个对象,你正在做一个
soundByName.soundName = new Sound()
=>你正在soundByName中创建一个名为soundName的字段。
使用soundByName [soundName] = new Sound();
这意味着创建一个名称取自变量coundName的字段。
soundByName is an Object and you are doing a
soundByName.soundName=new Sound()
=> you are creating a field named soundName within soundByName. UsesoundByName[soundName]=new Sound();
which mean create a field with the name taken from the variable coundName.
您正在使用channelByName使用 channelByName [channelName] = value;
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]);
}
这篇关于Actionscript从String Array加载/调用声音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!