简体   繁体   English

AS3键盘移动错误

[英]AS3 Keyboard Movement Error

Im making a game and have written the code to move my sprite with the arrow keys. 我正在制作游戏,并编写了使用箭头键移动精灵的代码。 For some reason, these two errors pop up. 由于某些原因,会弹出这两个错误。

LINE 34 Warning: 1090: Migration Issue: The onKeyDown event handler is not triggered automatically by Flash Player at run time in Actionscribpt 3.0. LINE 34警告:1090:迁移问题:onKeyDown事件处理程序不会在Actionscribpt 3.0中在运行时由Flash Player自动触发。 You must first register this handler for the event using addEventListener ('keyDown' callback_handler). 您必须首先使用addEventListener('keyDown'callback_handler)为事件注册此处理程序。

LINE 39 Warning: 1090: Migration Issue: The onKeyUp event handler is not triggered automatically by Flash Player at run time in Actionscribpt 3.0. LINE 39警告:1090:迁移问题:onKeyUp事件处理程序不会在Actionscribpt 3.0中在运行时由Flash Player自动触发。 You must first register this handler for the event using addEventListener ('keyUp' callback_handler). 您必须首先使用addEventListener('keyUp'callback_handler)为事件注册此处理程序。

This is my code for the movement 这是我的运动代码

stop();

import flash.ui.Keyboard;
import flash.events.Event;
import flash.events.KeyboardEvent;

Wizard.addEventListener(Event.ENTER_FRAME, KeyClick);
stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);

var keys:Array = [];

function KeyClick(e:Event):void
{
    if (keys[Keyboard.RIGHT])
    {
        Wizard.x += 5;
    }
    if (keys[Keyboard.LEFT])
    {
        Wizard.x -= 5;
    }
    if (keys[Keyboard.UP]) 
    {
        Wizard.y -= 5;
    }
    if (keys[Keyboard.DOWN])
    {
        Wizard.y += 5;
    }
}

function onKeyDown(e:KeyboardEvent):void
{
    keys[e.keyCode] = true;
}

function onKeyUp(e:KeyboardEvent):void
{
    keys[e.keyCode] = false;
}

How do i fix these errors? 我该如何解决这些错误? Thanks :) 谢谢 :)

Those are just warnings; 这些只是警告; the reason they're there is because in ActionScript 2, events worked like: 之所以存在,是因为在ActionScript 2中,事件的工作方式如下:

target.onKeyUp = function()
{
    //
}

target.onKeyDown = function()
{
    //
}

I assume the warnings are triggered because you've used the same naming convention for your handler functions as the old style of handling these events. 我假设警告已触发,因为您对处理程序函数使用的命名约定与处理这些事件的旧样式相同。

If they bother you, simply rename them to something else. 如果它们困扰您,只需将其重命名为其他名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM