2014-07-23 74 views
0

我正在使用Flash CS4在舞臺邊界內移動動畫角色。生成SWF文件時沒有代碼錯誤,並且在Flash中測試影片時字符移動正常。發佈文件後,角色完全無法響應箭頭鍵。我不知道這個問題可能會有什麼幫助,將不勝感激。這裏是電影的代碼:SWF文件發佈後未檢測到箭頭鍵

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; 
     } 

    } 
} 
+0

如果您在瀏覽器窗口中運行你的SWF力的焦點,您可能需要點擊閃光物體上,然後才能接受鍵盤輸入。 – Marcela

回答

0

您可能需要一些JavaScript添加到您的HTML將焦點設置Flash對象上:

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

確保您更改movieID,無論你實際設置爲您的ID。

+0

不幸的是,這不起作用,我仍然必須先點擊我的電影,然後運行OK。這是迫使焦點的唯一方法還是有辦法在Flash中直接編寫代碼?恐怕我對HTML或JS不太舒服。 –

+0

它不起作用,因爲我的初始對象標籤包含一個空格。所以我改了名字,現在可以正常工作了,謝謝。 –

0

,你可以通過做

this.stage.focus = myTextField; // assuming this is added to the display hierarchy