AS3中键组合的EventListener?

时间:2016-06-02 19:41:11

标签: actionscript-3 flash animation actionscript

我试着找出条件和"捷径"从flash中退出独立应用程序。我想推两个键和这两个键的组合" C + M"应该退出我的应用程序。 继承我的代码,但它仍然没有工作。我试着让应用程序允许我同时按下多个按钮,然后我创建了退出功能。 任何答案都很棒。



var keyPressedC:Boolean;  
var keyPressedM:Boolean;       

addEventListener(KeyboardEvent.KEY_DOWN, check_key_down,false,0,true);
addEventListener(KeyboardEvent.KEY_UP, check_key_up,false,0,true);
addEventListener(Event.ENTER_FRAME, check_keys,false,0,true);

function check_keys(event:Event):void
{
    if(keyPressedC)
      trace("pushed C")
    if(keyPressedM)
      trace("pushed M")
}

function check_key_down(event:KeyboardEvent):void
{
    if(event.keyCode == 67)
        keyPressedC = true;
    if(event.keyCode == 77)
        keyPressedM = true;
   }

function check_key_up(event:KeyboardEvent):void
{
    if(event.keyCode == 67)
        keyPressedC = false;
    if(event.keyCode == 77)
        keyPressedM = false;
}
   
import flash.system.fscommand;

stage.addEventListener(KeyboardEvent.KEY_DOWN, enterKeyHandlercm);
 function enterKeyHandlercm(event:KeyboardEvent):void 
{
	if (event.keyCode == Keyboard.C && event.keyCode == Keyboard.M)
	{
		fscommand("quit");
	}
}




编辑,仍然无法正常工作:



var keyPressedC:Boolean;  
var keyPressedM:Boolean;       

addEventListener(KeyboardEvent.KEY_DOWN, check_key_down,false,0,true);
addEventListener(KeyboardEvent.KEY_UP, check_key_up,false,0,true);
addEventListener(Event.ENTER_FRAME, check_keys,false,0,true);

function check_keys(event:Event):void
{
    if(keyPressedC)
      trace("pushed C")
    if(keyPressedM)
      trace("pushed M")
}

function check_key_down(event:KeyboardEvent):void
{
    if(event.keyCode == 67)
        keyPressedC = true;
    if(event.keyCode == 77)
        keyPressedM = true;
   }

function check_key_up(event:KeyboardEvent):void
{
    if(event.keyCode == 67)
        keyPressedC = false;
    if(event.keyCode == 77)
        keyPressedM = false;
}
   
import flash.system.fscommand;

stage.addEventListener(KeyboardEvent.KEY_DOWN, enterKeyHandlercm);
 function enterKeyHandlercm(event:KeyboardEvent):void 
{
	if (keyPressedM == true && keyPressedC == true)
	{
		fscommand("quit");
	}
}




1 个答案:

答案 0 :(得分:1)

enterKeyHandlercm块中,您的逻辑应该是评估keypressed值,而不是keyCode值。

function enterKeyHandlercm(event:KeyboardEvent):void 
{
    if (keyPressedM == true && keyPressedC == true)
    {
        fscommand("quit");
    }
}

使用此代码,为您的5种关键可能性中的每一种添加不同的MC(c up,c down,m up,m down,c + m down)。

package {
    import flash.display.*;
    import flash.events.*;
    import flash.system.fscommand;
    import flash.system.System; //add this if you try System.exit(0);

    public class FlashTest extends MovieClip 
    {

        public function FlashTest() 
        {
            var keyPressedC:Boolean;  
            var keyPressedM:Boolean;       

            // need to add eventListener to stage
            // default values work fine.  
            stage.addEventListener(KeyboardEvent.KEY_DOWN, check_key_down);
            stage.addEventListener(KeyboardEvent.KEY_UP, check_key_up);
            stage.addEventListener(Event.ENTER_FRAME, check_keys);

            function check_key_down(event:KeyboardEvent):void
            {
                if(event.keyCode == 67)
                {
                    keyPressedC = true;
                    newBall(-100);
                }
                if(event.keyCode == 77)
                {
                    keyPressedM = true;
                    newBall();
                }
             }

             function check_key_up(event:KeyboardEvent):void
             {
                 if(event.keyCode == 67)
                 {
                     keyPressedC = false;
                     newBall(-50);
                 }
                 if(event.keyCode == 77)
                 {
                     keyPressedM = false;
                     newBall(50);
                 }
             }

             function enterKeyHandlercm(event:KeyboardEvent):void 
             {
                 if (keyPressedM == true && keyPressedC == true)
                 {
                     newBall(100); 
                     fscommand("quit");
                     // or try System.exit(0);
                 }
              }


              function newBall(x:Number=0):void
              {
                      var ball:Sprite = new Sprite();
                      ball.graphics.lineStyle();
                      ball.graphics.beginFill(0x000000);
                      ball.graphics.drawCircle(0,0,20);
                      ball.graphics.endFill();
                      addChild(ball);
                      ball.x = stage.stageWidth/2+x;
                      ball.y = stage.stageHeight/2;
              }      
          }
      }
  }

请原谅我的冗长,但这样我们就不会遗漏任何东西。我添加球构造函数的原因是因为我只有我的笔记本电脑,所以我不得不使用在线IDE,我不知道如何找到输出窗口或运行调试器,它不需要系统命令。但是我可以通过ball方法确认的是,当“c”“m”被一起按下时,会实例化一个唯一的MC。这意味着我们的代码现在会在同时按下两个键时使flash注册一个唯一的事件。