2017-01-23 87 views
0

我正在爲我的遊戲編程課程開發DDR遊戲,並且我能夠使用鼠標使用箭頭。但要求也是使用鍵盤也可以使用它。我無法準確使用鍵盤來使用它。AS3將MouseEvent轉換爲KeyboardEvent

這是我的源代碼,我將如何轉換MouseEvent的使用KeyboardEvents的上,下,左,右按鈕?

import flash.events.KeyboardEvent; 
import flash.display.MovieClip; 
import flashx.textLayout.operations.ModifyInlineGraphicOperation; 
import flash.events.MouseEvent; 
import flash.events.Event; 
import flash.net.URLRequest; 
import flash.media.Sound; 
import flash.media.SoundChannel; 

var pattern = new Array(); 
var buttons = new Array(); 
buttons.push(up, bottom, left, right); 
var position = 0; 
var playersTurn = false; 
var mc_starttext:MovieClip; 
var mc_background:MovieClip; 

//generate the pattern 
setTimeout(nextMove, 1000); // call after 1 second 

// Expecting click from they keyboard 

up.addEventListener(MouseEvent.CLICK, clicked); 
bottom.addEventListener(MouseEvent.CLICK, clicked); 
left.addEventListener(MouseEvent.CLICK, clicked); 
right.addEventListener(MouseEvent.CLICK, clicked); 

stage.addEventListener(KeyboardEvent.KEY_DOWN, onkeyPress); 
stage.addEventListener(KeyboardEvent.KEY_UP, onkeyRelease); 


mc_starttext.buttonMode = true; 
mc_starttext.addEventListener(MouseEvent.CLICK, startClick) 
mc_background.buttonMode = true; 
mc_background.addEventListener(MouseEvent.CLICK, startClick) 


function startClick(e:MouseEvent):void{ 
      dispatchEvent(new Event("START_GAME")); 
     } 

function hideScreen():void{ 
      this.visible = false; 
     } 

function showScreen():void{ 
      this.visible = true; 
     } 

function onkeyPress(event:KeyboardEvent):void{ 

    if (event.keyCode == 13)//enter 
       { 
        this.mc_background.visible = false 
        this.mc_starttext.visible = false 
        //this.StartCover.visible = false; 
        //this.StartText.visible = false; 

        //this.score.text = position.toString(); 
        //this.score.visible = true; 

        //startPlay = true; 
        setTimeout(nextMove, 2000);//Call nextmove after two second 

       } 

    if (event.keyCode == 32) 
    { 
     trace("space bar"); 
    } 
} 

function onkeyRelease(event:KeyboardEvent):void{ 
    if (event.keyCode == 32){ 
     trace("space release"); 
    } 
} 

function clicked(clickInfo:MouseEvent){ 

    if(!playersTurn) return; 

    if (clickInfo.target == pattern[position]){ 
     trace("right"); 
     position = position + 1; 
     //Check to see if it is computers turn 
     if (position == pattern.length){ 
      //CPUs turn 
      position = 0; 
      setTimeout(nextMove, 1000) 
     } 
     // play button animation 
     clickInfo.target.gotoAndPlay(2); 
    } else { 
     trace("wrong"); 
    } 

} 

function nextMove(){ 

    if (position < pattern.length){ 
     pattern[position].play(); 
     position++; 
     setTimeout(nextMove, 1000); 
    } else { 
     // Generate random number 
     var randomNumber = Math.floor(Math.random()*4); 
     pattern.push(buttons[randomNumber]); 
     buttons[randomNumber].play(); 
     playersTurn = true; 
     position = 0; 
    } 
} 
+0

現在解決了這個問題還是需要將一個功能連接到鼠標和鍵盤上? –

+0

我其實還沒有解決它!這是週一到期,我正在研究其他課程,因爲我被卡住了。我無法弄清楚如何讓它適用於鼠標和鍵盤。 @ VC.One – DaveTheCoder

回答

0

查看此演示代碼是否有助於您瞭解如何使用一個函數來處理KeyboardMouse事件。

只是測試用...

  • 做一個矩形(舞臺上繪製或通過代碼創建)。
  • 給它的實例名稱:

然後在下面添加代碼。這是自我解釋,但要問你需要什麼。希望能幫助到你。

//variable "box" is a rectangle on Stage with instance name "box" 

//# Both Mouse & Keyboard events point to one "control_Move" function 
stage.addEventListener(KeyboardEvent.KEY_DOWN, control_Move); 
box.addEventListener(MouseEvent.CLICK, control_Move); 

//# Make function of generic-ish Event type 
function control_Move (evt:Event) : void 
{ 
    //trace ("evt.type : " + evt.type); 

    if (evt.type == "click") //# If mouse clicked 
    { 
     //# If click target is "box" then move "box" forward +5 pixels 
     if (evt.target.name == "box") { box.x += 5; } 

    } 

    if (evt.type == "keyDown") //# If keyboard pressed 
    { 
     //# LEFT ARROW KEY 
     if (KeyboardEvent(evt).keyCode == 37) { box.x -= 5; } 

     //# RIGHT ARROW KEY 
     if (KeyboardEvent(evt).keyCode == 39) { box.x += 5; } 

    } 

} 
+0

謝謝!我只是跑了它,它像你說的那樣工作。我將採取這些知識並實施它來做我所需要的事情!我非常感謝幫助! @ VC.One – DaveTheCoder

3

簡單。既然你已經有一個KeyboardEvent.KEY_DOWN聽衆,做一個函數:

onkeyPress(e) 
    { 

    } 

在該功能附加「如果」語句測試鍵碼,例如:

if(e.keyCode == 37)  //this is the code for the left arrow key 
    { 


    } 

然後告訴你的程序是什麼您希望在按下該鍵時執行此操作。

你應該能夠自己找出其餘的。

+0

非常感謝!說得通! – DaveTheCoder

+0

我想我的問題並不清楚,但我早些時候做過,但我的意思是我的按鈕(上,下,右,左)被設置爲MouseEvents,它被髮送到點擊函數,我將如何改變這與鍵盤事件一起工作?我有點新,所以我知道做up.addEventListener(KeyboardEvent.KEY_DOWN,onKeyPress);不會工作@克雷格 – DaveTheCoder

+0

我不完全理解你,DaveTheCoder。你問一個按鈕對象或MovieClip對象如何分派鍵盤事件? – Craig