Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 需要帮助使按钮在Adobe Flash中播放多种声音吗_Actionscript 3_Flash_Button_Audio - Fatal编程技术网

Actionscript 3 需要帮助使按钮在Adobe Flash中播放多种声音吗

Actionscript 3 需要帮助使按钮在Adobe Flash中播放多种声音吗,actionscript-3,flash,button,audio,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:Sou

我在为我的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 ();}
}

有没有办法重写它以容纳多个按钮?前半部分一直工作到在“导入flash”中重复。我一直在到处寻找答案,所以请帮助

您不应该将两个函数或两个变量命名为相同的名称,因为最后一个将覆盖前一个

比如说

var request:URLRequest = new URLRequest("19103_b.mp3");
然后被覆盖

var request:URLRequest = new URLRequest("Readya.mp3");
函数侦听器mouseDownHandler中的按钮ready\u H3AR也会发生同样的情况

顺便说一下,您不需要导入两次类:

import flash.utils.Dictionary;
import flash.events.MouseEvent;

通常,当一个任务或一段代码打算被多次使用时,最好将其放入函数中,以获得更简单、更可维护的代码

所以你可以这样做:

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

希望这能有所帮助。

好的,我想我会删除重复的功能。我确实删除了!非常感谢。将声音放入阵列是我一直在寻找的。