2013-12-18 50 views
0

即時通訊按下按鈕即可從圖書館播放移動圖片,而且我已將其與圖片鏈接起來,但仍然無效(這只是一小部分代碼有事件偵聽器和處理程序到位,角色移動罰款)如何從圖書館播放移動圖片,並按下按鍵

stage.addEventListener(Event.ENTER_FRAME, Move); 
stage.addEventListener(KeyboardEvent.KEY_DOWN, keyPress); 
stage.addEventListener(KeyboardEvent.KEY_UP, keyUp); 

var aP:Boolean = false; 
var dP:Boolean = false; 
//creating a new Character_right instance 
var character_right:Character_right = new Character_right(); 

function Move(vet:KeyboardEvent) 
{ 
    if(aP) 
    { 
     char.x -= 5; 
     char.scaleX = -0.55; 
    } 
    if(dP) 
    { 
     char.x += 5; 
     char.scaleX = -0.55; 
    } 
} 

function keyPress(evt:KeyboardEvent) 
{ 
    switch(evt.keyCode) 
    { 
     case Keyboard.A: 
     { 
     aP = true; 
     break; 
     } 
     case Keyboard.D: 
     { 
     dP = true; 
     character_right.play(); 
     break; 
     } 
    } 
} 

function keyUp(evt:KeyboardEvent) 
{ 
     switch(evt.keyCode) 
    { 
     case Keyboard.A: 
     { 
     aP = false; 
     break; 
     } 
     case Keyboard.D: 
     { 
     dP = false; 
     break; 
     } 
    } 
} 
+0

...你正在'Character_right.play()'之前執行'break' ......當然這不起作用。 – Cilan

+0

你想做什麼,Character_right是一個電影剪輯?你有錯誤嗎? – Cilan

+0

是的,它是一個movieclip,我有AS鏈接它,它說以下錯誤: – Smadygrady

回答

0

檢查this post,與有關鍵盤事件的一個很好的教程中,我相信將是重要的,你理解的概念,並在你的邏輯實現。

簡而言之就像下面的例子,但記住要爲你的範圍實現所有必要的邏輯。

 var isRight:Boolean; 
     var isLeft:Boolean; 
     var isUp:Boolean; 
     var isDown:Boolean; 
     //creating a new Character_right instance 
     var character_right:Character_right = new Character_right(); 

     stage.addEventListener(KeyboardEvent.KEY_DOWN, downKeyHandler, false, 0, true); 
     function downKeyHandler(event:KeyboardEvent):void 
     { 
      if (event.keyCode == Keyboard.RIGHT) 
      { 
       isRight = true; 
      } 
      if (event.keyCode == Keyboard.LEFT) 
      { 
       isLeft = true; 
      } 
      if (event.keyCode == Keyboard.UP) 
      { 
       isUp = true; 
      } 
      if (event.keyCode == Keyboard.DOWN) 
      { 
       isDown = true; 
      } 
     } 

     stage.addEventListener(KeyboardEvent.KEY_UP, upKeyHandler, false, 0, true); 
     function upKeyHandler(event:KeyboardEvent):void 
     { 
      if (event.keyCode == Keyboard.RIGHT) 
      { 
       isRight = false; 
      } 
      if (event.keyCode == Keyboard.LEFT) 
      { 
       isLeft = false; 
      } 
      if (event.keyCode == Keyboard.UP) 
      { 
       isUp = false; 
      } 
      if (event.keyCode == Keyboard.DOWN) 
      { 
       isDown = false; 
      } 
     } 

     stage.addEventListener(Event.ENTER_FRAME, loopHandler, false, 0, true); 
     function loopHandler(event:Event):void 
     { 
      if (isRight) 
      { 
       //do something here… 
            character_right.play(); 
          } 
      if (isLeft) 
      { 
       //do something here... 
      } 
      if (isUp) 
      { 
       //do something here... 
      } 
      if (isDown) 
      { 
       //do something here... 
      } 
     } 
+0

解釋'這個職位',不要只發佈一個鏈接... – Cilan

+0

謝謝,我剛剛編輯我的答案。 – gabriel

+0

這並不能真正幫助我解決我的問題,但不適合編輯我的問題,以便您更好地理解它。 – Smadygrady

0

在代碼中,你break後執行功能Character_right.play()和休息停止從正在執行的循環代碼的休息,所以break之前執行Character_right.play(),例如:

function keyPress(evt:KeyboardEvent) 
{ 
    switch(evt.keyCode) 
    { 
     case Keyboard.A: 
     { 
     aP = true; 

     break; 
     } 
     case Keyboard.D: 
     { 
     dP = true; 
     Character_right.play(); 
     break; 
     } 
    } 
} 
+0

它仍然不起作用 – Smadygrady

+0

對任何讀這個答案的人來說:在問題中,break是放在Character_right.play ),他只是編輯了他的問題。 – Cilan