如何使用鼠标单击事件将特定键事件分配给AS3中的此按钮数组

时间:2013-02-18 15:24:06

标签: actionscript-3

我正在创建一个虚拟鼓组件,它使用特定的外部声音按钮,可在调用鼠标事件和键事件时播放。目前我让鼠标事件正常工作。我无法弄清楚如何将特定的键事件(使用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();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

首先,您的键盘事件绑定到对象而不是舞台。舞台接收这些事件,因此您需要在那里添加事件的监听器。

我整合了您的代码。值得注意的是,我将每个鼓声的按钮,声音文件路径和击键绑定到soundArray中的相同索引。优点是,当您需要将按钮/按键与相应的声音文件配对时,您的数据已经在一起,从而使回放(和设置)更容易维护。

我现在无法测试,并且是徒手写的,所以一定要测试。希望它有所帮助。

-Cheers

// Replace the mouse with Drumsticks.
stage.addEventListener(Event.ENTER_FRAME, MoveMouse);
Mouse.hide();
function MoveMouse(Event) {
    drum_stick.x = mouseX;
    drum_stick.y = mouseY;
}

// The array now holds the button objects, the filename, and the corresponding keys.
var soundArray:Array = [
    { "btn": butt1, "file": 't.mp3', "key": 84 },
    { "btn": butt2, "file": 'r.mp3', "key": 82 },
    { "btn": butt3, "file": 'p.mp3', "key": 80 },
    { "btn": butt4, "file": 'o.mp3', "key": 79 },
    { "btn": butt5, "file": 'e.mp3', "key": 69 },
    { "btn": butt6, "file": 'i.mp3', "key": 73 },
    { "btn": butt7, "file": 'u.mp3', "key": 85 },
    { "btn": butt8, "file": 'w.mp3', "key": 87 },
    { "btn": butt9, "file": 'y.mp3', "key": 89 },
    { "btn": butt10, "file": 'q.mp3', "key": 81 } ]

//This adds the mouse click event to the buttons. 
for each (var item:Object in soundArray) {
    item.btn.addEventListener(MouseEvent.CLICK, buttonClicked);
}
// This was registered to a button.  It needs to be on the stage.
stage.addEventListener(KeyboardEvent.KEY_DOWN, tsymbolkeyhit);

function tsymbolkeyhit(e:KeyboardEvent):void {
    // Handles playing the sound when hitting keyboard buttons.
    for each (var item:Object in soundArray) {
        // If the key we hit matches the keystroke in the array, play the appropriate sound, and break the loop.
        if (item.key == e.keyCode) {
            playKey(item.file);
            break;
        }
    }
}

function buttonClicked(e:MouseEvent):void {
    // Handles playing sound when hitting onscreen buttons.
    for each (var item:Object in soundArray) {
        // If the button we clicked matches the button in the array, play the appropriate sound, and break the loop.
        if (item.btn == e.currentTarget) {
            playKey(item.file);
            break;
        }
    }
}

function playKey(filePath:String):void {
    // Simply plays the specified file.
    var snd:Sound = new Sound();
    var channel:SoundChannel = new SoundChannel();
    snd.load(new URLRequest(filePath));
    channel = snd.play();
}

var mySound:Sound = new Sound();
mySound.load(new URLRequest("acapella.mp3"));
var myChannel:SoundChannel = new SoundChannel();
clicktoplaymusic.addEventListener(MouseEvent.CLICK, playSound);

function playSound(event:MouseEvent):void {
    myChannel = mySound.play();
}

pausebutton.addEventListener(MouseEvent.CLICK, stopSound);
function stopSound(event:MouseEvent):void {
    channel.stop();
}