我正在为我的游戏编程课程开发DDR游戏,并且我能够使用鼠标让箭头工作。但要求也是使用键盘也可以使用它。我无法使用键盘让它完全正常工作。
这是我的源代码,如何将MouseEvent转换为使用KeyboardEvents为up,bottom,left和right按钮工作?
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 :(得分:3)
易。由于您已经有 KeyboardEvent.KEY_DOWN 侦听器,请创建一个函数:
onkeyPress(e)
{
}
在该函数中添加'if'语句测试密钥代码,例如:
if(e.keyCode == 37) //this is the code for the left arrow key
{
}
然后告诉你的程序在按下该键时你想要它做什么。
你应该能够自己弄清楚其余部分。
答案 1 :(得分:0)
查看此演示代码是否有助于您了解如何使用一个函数来处理Keyboard
和Mouse
事件。
试试......
然后在下面添加代码。这是自我解释,但问你需要什么。希望它有所帮助。
//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; }
}
}