0

我想知道当一个键事件附加到它时,如何使用 actionscript 3 在 Flash 中制作一个按钮动画。我可以使用鼠标单击事件(当您双击按钮并创建向上、向上和向下更改时)制作简单的动画,使按钮更改形状,但是当键盘事件附加到它时,按钮只是它的功能和没有动画......有人知道我如何为我的关键事件制作动画吗?

//initialise variables with functions
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
{
myChannel.stop();
}

 // The array now holds the drum 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
{
//plays my specified file.
var snd:Sound = new Sound();
var channel:SoundChannel = new SoundChannel();
snd.load(new URLRequest(filePath));
channel = snd.play();
}

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

1 回答 1

0

代码运行起来就像一台 rube goldberg 机器。你有一行由鼠标事件触发的事件,现在你只需要通过键盘事件对同一系列事件进行排队。

这是一个简单的例子:

button.addEventListener(MouseEvent.MOUSE_DOWN, btnEvent);
button.addEventListener(MouseEvent.MOUSE_UP, btnEvent);
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyEvent);
stage.addEventListener(KeyboardEvent.KEY_UP, keyEvent);

function btnEvent(e:MouseEvent):void {
    switch (e.type) {
        case "mouseDown":
            animateBtn("down");
            break;
        case "mouseUp":
            animateBtn("up");
            break;
    }
}

function keyEvent(e:KeyboardEvent):void {
    switch (e.type) {
        case "keyDown":
            animateBtn("down");
            break;
        case "keyUp":
            animateBtn("up");
            break;
    }
}

function animateBtn(state:String):void {
    if (state == "down") {
        button.alpha = 0.5;
    } else if (state == "up") {
        button.alpha = 1;
    }
}
于 2013-02-18T19:49:06.653 回答