需要帮助使按钮在Adobe Flash中播放多个声音

时间:2015-07-06 00:01:46

标签: actionscript-3 flash button audio

我在尝试让我的代码适用于我的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 ();}
}

任何重写此方法以容纳多个按钮的方法?上半部分工作直到它在“导入闪存”中重复出现。我一直在寻找答案,所以请帮助!!

2 个答案:

答案 0 :(得分:0)

你不应该将两个函数或两个变量命名为相同,因为最后一个将覆盖第一个函数。

例如

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;

答案 1 :(得分:0)

通常,当任务或代码块打算多次使用时,最好将它放在函数中以获得更简单,更易于维护的代码。

所以你可以这样做:

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

希望可以提供帮助。