需要帮助让按钮在 Adobe Flash 中播放多种声音
Posted
技术标签:
【中文标题】需要帮助让按钮在 Adobe Flash 中播放多种声音【英文标题】:Need help making a button play multiple sounds in Adobe Flash 【发布时间】:2015-09-23 00:44:08 【问题描述】:我在尝试让我的代码为我的 Adobe Flash 项目工作时遇到问题。基本上,我试图让一个按钮在每次单击它时播放随机声音,这很有效,但我不能在同一帧上为不同的按钮使用相同的代码,这就是它给我这个错误的原因。
这是我的代码:
import flash.utils.Dictionary;
import flash.events.MouseEvent;
var request:URLRequest = new URLRequest("19103_b.mp3");
var ci_diese:Sound = new Sound();
ci_diese.load(request);
var request_two:URLRequest = new URLRequest("19203_b.mp3");
var d_diese:Sound = new Sound();
d_diese.load(request_two);
var request_three:URLRequest = new URLRequest("19204_b.mp3");
var f_diese:Sound = new Sound();
f_diese.load(request_three);
var play_liste = 0;
var dictSounds = new Dictionary ();
dictSounds[1] = d_diese;
dictSounds[2] = ci_diese;
dictSounds[3] = f_diese;
fireweapon_H3AR.addEventListener (MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler (event:MouseEvent) : void
play_liste = Math.ceil(Math.random () *3);
dictSounds[play_liste].play ();
ready_H3AR.addEventListener (MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler (event:MouseEvent) : void
play_liste = Math.ceil(Math.random () *3);
dictSounds[play_liste].play ();
import flash.utils.Dictionary;
import flash.events.MouseEvent;
var request:URLRequest = new URLRequest("Readya.mp3");
var ci_diese2:Sound = new Sound();
ci_diese2.load(request);
var request_two:URLRequest = new URLRequest("Readyb.mp3");
var d_diese2:Sound = new Sound();
d_diese2.load(request_two);
var request_three:URLRequest = new URLRequest("Readyc.mp3");
var f_diese2:Sound = new Sound();
f_diese2.load(request_three);
var play_liste = 0;
var dictSounds = new Dictionary ();
dictSounds[1] = d_diese2;
dictSounds[2] = ci_diese2;
dictSounds[3] = f_diese2;
ready_H3AR.addEventListener (MouseEvent.MOUSE_DOWN, mouseDownHandler);
function mouseDownHandler (event:MouseEvent) : void
play_liste = Math.ceil(Math.random () *3);
dictSounds[play_liste].play ();
有什么方法可以重写它以容纳多个按钮?前半部分一直持续到它在“导入闪存”处重复。我一直在到处寻找答案,所以请帮忙!
【问题讨论】:
【参考方案1】:您不应将两个函数或两个变量命名为相同,因为最后一个会覆盖第一个。
例如
var request:URLRequest = new URLRequest("19103_b.mp3");
然后被覆盖
var request:URLRequest = new URLRequest("Readya.mp3");
按钮 ready_H3AR 和函数监听器 mouseDownHandler 也会发生同样的情况
顺便说一句,你不需要两次导入类:
import flash.utils.Dictionary;
import flash.events.MouseEvent;
【讨论】:
好的,我想我会删除重复的函数【参考方案2】:通常当一个任务或一段代码打算被多次使用时,最好把它放在一个函数中,以获得更简单和更易于维护的代码。
所以你可以这样做:
var sounds:Array = [
['01.mp3', '02.mp3', '03.mp3'], // sounds for the 1st button
['04.mp3', '05.mp3', '06.mp3'] // sounds for the 2nd button
];
var sound:Sound,
sound_channel:SoundChannel = new SoundChannel();
function play_sound(sound_name:String): void
var request:URLRequest = new URLRequest('mp3/' + sound_name);
sound = new Sound();
sound.addEventListener(Event.COMPLETE, on_sound_loaded);
sound.load(request);
function on_sound_loaded(e:Event): void
// if you want, you can stop the current playing sound
// otherwise you don't need the SoundChannel
sound_channel.stop();
sound_channel = Sound(e.target).play();
button_01.addEventListener(MouseEvent.MOUSE_DOWN, on_press);
button_02.addEventListener(MouseEvent.MOUSE_DOWN, on_press);
function on_press(e:MouseEvent): void
// to get values : 0, 1 or 2
var random:int = int(Math.random () * sounds[0].length);
// if it's the 1st button so pick the sound from the 1st sounds array
if(e.currentTarget.name == 'button_01')
play_sound(sounds[0][random]);
else
play_sound(sounds[1][random]);
希望能有所帮助。
【讨论】:
确实做到了!谢谢!将声音放入数组中正是我想要的。以上是关于需要帮助让按钮在 Adobe Flash 中播放多种声音的主要内容,如果未能解决你的问题,请参考以下文章
如果我不断循环播放 Adobe Animate (Flash) 视频,会导致内存问题吗?
AS3 如何将 adMob 添加到 Adobe Flash/Animate?
如何使用 CefSharp 启用 Adobe Flash?