Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 AS3将MouseeEvent转换为KeyboardEvent_Actionscript 3_Flash_Mouseevent_Keyboard Events_Flash Cs6 - Fatal编程技术网

Actionscript 3 AS3将MouseeEvent转换为KeyboardEvent

Actionscript 3 AS3将MouseeEvent转换为KeyboardEvent,actionscript-3,flash,mouseevent,keyboard-events,flash-cs6,Actionscript 3,Flash,Mouseevent,Keyboard Events,Flash Cs6,我正在为我的游戏编程课程做一个DDR游戏,我能够用鼠标让箭头工作。但一个要求是也要让它使用键盘工作。我不能用键盘让它正常工作 这是我的源代码,我如何将MouseEvent转换为使用键盘事件的向上、向下、向左和向右按钮 import flash.events.KeyboardEvent; import flash.display.MovieClip; import flashx.textLayout.operations.ModifyInlineGraphicOperation; import f

我正在为我的游戏编程课程做一个DDR游戏,我能够用鼠标让箭头工作。但一个要求是也要让它使用键盘工作。我不能用键盘让它正常工作

这是我的源代码,我如何将MouseEvent转换为使用键盘事件的向上、向下、向左和向右按钮

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;
    }
}
导入flash.events.KeyboardEvent;
导入flash.display.MovieClip;
导入flashx.textLayout.operations.ModifyInlineGraphicOperation;
导入flash.events.MouseEvent;
导入flash.events.Event;
导入flash.net.URLRequest;
导入flash.media.Sound;
导入flash.media.SoundChannel;
var pattern=新数组();
var按钮=新数组();
按钮。按下(上、下、左、右);
var位置=0;
var playersTurn=false;
var mc_starttext:MovieClip;
var mc_背景:MovieClip;
//生成模式
设置超时(下一个移动,1000);//1秒钟后打电话
//期望从他们的键盘点击
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\u UP,onkeyRelease);
mc_starttext.buttonMode=true;
mc_starttext.addEventListener(MouseEvent.CLICK,startClick)
mc_background.buttonMode=true;
mc_background.addEventListener(MouseEvent.CLICK,startClick)
函数开始单击(e:MouseeEvent):无效{
dispatchEvent(新事件(“开始游戏”));
}
函数hideScreen():void{
可见=假;
}
函数showScreen():void{
可见=真实;
}
按键功能(事件:键盘事件):无效{
if(event.keyCode==13)//输入
{
this.mc_background.visible=false
this.mc_starttext.visible=false
//this.startOver.visible=false;
//this.StartText.visible=false;
//this.score.text=position.toString();
//this.score.visible=true;
//startPlay=true;
setTimeout(nextMove,2000);//两秒钟后调用nextMove
}
如果(event.keyCode==32)
{
跟踪(“空格键”);
}
}
函数onkeyRelease(事件:KeyboardEvent):无效{
如果(event.keyCode==32){
跟踪(“空间释放”);
}
}
单击功能(单击信息:MouseeEvent){
如果(!playersTurn)返回;
if(clickInfo.target==模式[位置]){
跟踪(“权利”);
位置=位置+1;
//检查一下,看是不是该轮到你了
if(位置==图案长度){
//CPU转向
位置=0;
设置超时(下一个移动,1000)
}
//播放按钮动画
点击info.target.gotoAndPlay(2);
}否则{
跟踪(“错误”);
}
}
函数nextMove(){
if(位置<图案长度){
模式[位置].play();
位置++;
设置超时(下一个移动,1000);
}否则{
//生成随机数
var randomNumber=Math.floor(Math.random()*4);
模式。按钮(按钮[随机数]);
按钮[randomNumber]。播放();
playersTurn=true;
位置=0;
}
}

简单。由于您已经有了一个键盘事件。请按下监听器,制作一个函数:

onkeyPress(e) 
   {

   }
在该功能中,添加测试关键代码的“如果”语句,例如:

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


     }
然后告诉你的程序,当按下该键时,你希望它做什么


你应该能够自己解决剩下的问题。

简单。由于您已经有了一个键盘事件。请按下监听器,制作一个函数:

onkeyPress(e) 
   {

   }
在该功能中,添加测试关键代码的“如果”语句,例如:

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


     }
然后告诉你的程序,当按下该键时,你希望它做什么


您应该能够自己解决其余的问题。

看看此演示代码是否有助于您理解如何使用一个函数来处理
键盘和
鼠标事件

只要测试一下

  • 制作一个矩形(在舞台上绘制或按代码创建)。
  • 为其指定实例名: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; } 

    }

}

查看此演示代码是否帮助您了解如何使用一个函数来处理
键盘
鼠标
事件

只要测试一下

  • 制作一个矩形(在舞台上绘制或按代码创建)。
  • 为其指定实例名: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; } 

    }

}

谢谢!有道理!我想我的问题不太清楚,但我之前确实这么做了,但我的意思是,我的按钮(上、下、右、左)设置为MouseEvents,然后发送到clicked函数,我如何更改它以处理键盘事件?我对这一点有点陌生,所以我知道怎么做。addEventListener(KeyboardEvent.KEY\u DOWN,onKeyPress);不行@CraigI我不太理解你,DaveTheCoder。您是在问button对象或MovieClip对象如何发送键盘事件吗?非常感谢!有道理!我想我的问题不太清楚,但我之前确实这么做了,但我的意思是,我的按钮(上、下、右、左)设置为MouseEvents,然后发送到clicked函数,我如何更改它以处理键盘事件?我对这一点有点陌生,所以我知道怎么做。addEventListener(KeyboardEvent.KEY\u DOWN,onKeyPress);不行@CraigI我不太理解你,DaveTheCoder。你是在问button对象或MovieClip对象如何发送键盘事件?这个问题现在解决了吗?还是你仍然需要将一个功能同时连接到鼠标和键盘?我实际上还没有做到