【问题标题】:AS3 Converting MouseEvent to KeyboardEventAS3 将鼠标事件转换为键盘事件
【发布时间】: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


【解决方案1】:

看看这个演示代码是否能帮助您了解如何使用一个函数来处理KeyboardMouse 事件。

只是测试...

  • 制作一个矩形形状(在舞台上绘制或通过代码创建)。
  • 给它实例名称:box

然后在下面添加代码。这是不言自明的,但可以询问您需要的任何内容。希望对您有所帮助。

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

    }

}

【讨论】:

  • 谢谢!我刚刚运行它,它就像你说的那样工作。我将利用这些知识并实施它来做我需要它做的事情!我真的很感激帮助! @VC.One
【解决方案2】:

简单。既然您已经有一个 KeyboardEvent.KEY_DOWN 监听器,请创建一个函数:

onkeyPress(e) 
   {

   }

在该函数中添加'if'语句测试关键代码,例如:

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


     }

然后告诉你的程序当那个键被按下时你想让它做什么。

其余的你应该可以自己解决。

【讨论】:

  • 非常感谢!有道理!
  • 我想我的问题不太清楚,但我之前确实这样做过,但我的意思是我的按钮(上、下、右、左)设置为发送到的 MouseEvents单击的功能,我将如何更改它以使用键盘事件?我对此有点陌生,所以我知道做 up.addEventListener(KeyboardEvent.KEY_DOWN, onKeyPress);不会工作@Craig
  • 我不太了解你,DaveTheCoder。您是在问按钮对象或 MovieClip 对象如何调度键盘事件?
猜你喜欢
  • 1970-01-01
  • 2011-10-25
  • 2011-07-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多