发布SWF文件后未检测到箭头键

时间:2014-07-23 13:48:26

标签: actionscript-3 flash

我使用Flash CS4让动画角色在舞台边界内移动。生成SWF文件时没有代码错误,当我在Flash中测试影片时,角色移动正常。发布文件后,该字符对箭头键完全没有响应。我不知道问题可能是什么,一些帮助将不胜感激。这是电影的代码:

stop();

var upPressed:Boolean = false;
var downPressed:Boolean = false;
var leftPressed:Boolean = false;
var rightPressed:Boolean = false;
var isWalking:Boolean = false;
var mySpeed:Number = 3;

my_Sprite.addEventListener(Event.ENTER_FRAME, moveSprite);
stage.addEventListener(KeyboardEvent.KEY_DOWN, setKeyPressed);
stage.addEventListener(KeyboardEvent.KEY_UP, unsetKeyPressed);

function moveSprite(event:Event):void {
    if(upPressed && my_Sprite.y >= 40){
        my_Sprite.y -= mySpeed;
    }
    if(downPressed && my_Sprite.y <= 440){
        my_Sprite.y += mySpeed;
    }
    if(leftPressed && my_Sprite.x >= 20){
        my_Sprite.x -= mySpeed;
    }
    if(rightPressed && my_Sprite.x <= 600){
        my_Sprite.x += mySpeed;
    }
}

function setKeyPressed(event:KeyboardEvent):void {
    switch(event.keyCode){
        case Keyboard.UP: {
            upPressed = true;
      if (!isWalking) {        
  my_Sprite.gotoAndPlay("Up");
              isWalking = true;
          }
          break;
       }

       case Keyboard.DOWN: {
          downPressed = true;
          if (!isWalking) {        
              my_Sprite.gotoAndPlay("Down");
              isWalking = true;
          }
          break;
       }

       case Keyboard.LEFT: {
           leftPressed = true;
           if (!isWalking) {           
               my_Sprite.gotoAndPlay("Left");
               isWalking = true;
          }
          break;
       }  

       case Keyboard.RIGHT: {
           rightPressed = true;
           if (!isWalking) {           
               my_Sprite.gotoAndPlay("Right");
               isWalking = true;
          }
          break;
       }  

    }
}


function unsetKeyPressed(event:KeyboardEvent):void {

    switch(event.keyCode) {

       case Keyboard.UP: {
                 upPressed = false;
           my_Sprite.gotoAndStop("Up");
           isWalking = false;
           break;
       }

       case Keyboard.DOWN: {
                 downPressed = false;
           my_Sprite.gotoAndStop("Down");
           isWalking = false;
           break;
       }

       case Keyboard.LEFT: {
                 leftPressed = false;
           my_Sprite.gotoAndStop("Left");
           isWalking = false;
           break;
       }

       case Keyboard.RIGHT: {
                 rightPressed = false;
           my_Sprite.gotoAndStop("Right");
           isWalking = false;
           break;
       }  

    }
}

2 个答案:

答案 0 :(得分:0)

您可能需要在HTML中添加一些JavaScript,以便将焦点设置在Flash对象上:

<body onLoad="window.document.movieID.focus();">

确保将movieID更改为您实际设置为ID的任何内容。

答案 1 :(得分:0)

你可以通过

来强制关注
this.stage.focus = myTextField; // assuming this is added to the display hierarchy