如何使用动作脚本3为附加了关键事件的按钮设置动画?

时间:2013-02-18 17:59:14

标签: actionscript-3

我想知道当关键事件附加到动作脚本3时,如何使用动作脚本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;
}

1 个答案:

答案 0 :(得分: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;
    }
}