我正在创建一个虚拟鼓组,它使用特定的外部声音按钮,在调用鼠标事件和按键事件时播放。目前我的鼠标事件工作正常。我不知道如何将特定的键事件(使用 T、R、P、O、E、I、U、W、Y、Q)添加到数组中的每个按钮。测试 butt1 我有一个问题是在允许关键事件之前单击按钮。这是我的代码:
//initialise variables
//Array for buttons instances.
var buttonsArray:Array = new Array();
buttonsArray[0] = butt1;
buttonsArray[1] = butt2;
buttonsArray[2] = butt3;
buttonsArray[3] = butt4;
buttonsArray[4] = butt5;
buttonsArray[5] = butt6;
buttonsArray[6] = butt7;
buttonsArray[7] = butt8;
buttonsArray[8] = butt9;
buttonsArray[9] = butt10;
//Array for the sound clip names.
var soundArray:Array = new Array();
soundArray[0] = 't.mp3';
soundArray[1] = 'r.mp3';
soundArray[2] = 'p.mp3';
soundArray[3] = 'o.mp3';
soundArray[4] = 'e.mp3';
soundArray[5] = 'i.mp3';
soundArray[6] = 'u.mp3';
soundArray[7] = 'w.mp3';
soundArray[8] = 'y.mp3';
soundArray[9] = 'q.mp3';
//This function doesn't work as i have to click the symbol before I can
//use a key event. Same for all other buttons in the array.
butt1.addEventListener(KeyboardEvent.KEY_DOWN, tsymbolkeyhit);
function tsymbolkeyhit(e:KeyboardEvent):void
{
if (e.keyCode == 84)
{
var s:Sound = new Sound();
s.load(new URLRequest(soundArray[0]));
s.play();
}
}
//playing acapella track with loading extertnal sound file
var my_sound:Sound = new Sound();
my_sound.load(new URLRequest("acapella.mp3"));
var my_channel:SoundChannel = new SoundChannel();
clicktoplaymusic.addEventListener(MouseEvent.CLICK, playSound);
function playSound(event:MouseEvent):void
{
my_channel = my_sound.play();
}
//pausing the acapella track
pausebutton.addEventListener(MouseEvent.CLICK, stopSound);
function stopSound(event:MouseEvent):void
{
my_channel.stop();
}
//custom mouse cursor;
Mouse.hide();
stage.addEventListener(Event.ENTER_FRAME, MoveMouse);
function MoveMouse(Event)
{
drum_stick.x = mouseX;
drum_stick.y = mouseY;
}
//This adds the mouse click event to the buttons.
for (var i:uint = 0; i < buttonsArray.length; i++)
{
buttonsArray[i].addEventListener(MouseEvent.CLICK, buttonClicked);
}
//plays the sound file thats clicked.;
function buttonClicked(e:MouseEvent):void
{
for (var i:uint = 0; i < buttonsArray.length; i++)
{
if (e.target == buttonsArray[i])
{
var s:Sound = new Sound();
s.load(new URLRequest(soundArray[i]));
s.play();
}
}
}