【发布时间】:2017-06-08 15:14:21
【问题描述】:
我正在为我的游戏编程课程开发一款 DDR 游戏,并且我能够使用鼠标使箭头工作。但一个要求是也让它使用键盘工作。我不能完全使用键盘让它工作。
这是我的源代码,如何将 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;
}
}
【问题讨论】:
-
这个问题现在解决了吗,还是您仍然需要将一个功能连接到鼠标和键盘?
-
其实我还没解决!星期一到期,我正在学习其他课程,所以推迟了,因为我被卡住了。我不知道如何让它同时适用于鼠标和键盘。 @VC.One
标签: actionscript-3 flash mouseevent keyboard-events flash-cs6