マウスイベントとキーイベントが呼び出されたときに再生される特定の外部サウンドボタンを使用する仮想ドラムキットを作成しています。現在、マウスイベントは正常に機能しています。特定のキーイベントを(T、R、P、O、E、I、U、W、Y、Qを使用して)配列の各ボタンに追加する方法がわかりません。バットのテスト1キーイベントが許可される前にボタンをクリックしないという問題があります。これが私のコードです:
//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();
}
}
}