Actionscript 3.0键盘事件侦听器错误

时间:2011-07-05 17:32:26

标签: actionscript-3 keydown event-listener

我有两个函数,rotate和unrotate,我使用键盘事件监听器来监听key_Down A和B但是我得到并且错误:

  

1119:通过带有静态类型Class的引用访问可能未定义的属性A.

ti.border = true
ti.addEventListener(TextEvent.TEXT_INPUT, onInput);

function onInput(event:TextEvent):void {
  if(ti.text.search('a')!=-1) load_image("http://i54.tinypic.com/anom5d.png", "ottefct");
  else if(ti.text.search('b')!=-1) load_image("http://i53.tinypic.com/2dv7dao.png", "rnd");
  else if(ti.text.search('c')!=-1) load_image("http://i51.tinypic.com/m8jp7m.png", "ssd");
}

var loaded_images:Dictionary = new Dictionary();

function load_image(url:String, id_name:String)
{
  var loader:Loader = new Loader();
  loader.name = id_name;
  var url_req:URLRequest = new URLRequest(url);
  loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadingComplete);
  loader.load(url_req);
}

function onLoadingComplete(evt:Event):void
{
  var img_name:String = evt.currentTarget.loader.name
  var spr_box:Sprite = new Sprite();
  spr_box.addChild(evt.currentTarget.loader);

  spr_box.mouseChildren = false;
  spr_box.doubleClickEnabled = true;

  spr_box.addEventListener(MouseEvent.MOUSE_DOWN, drag);
  spr_box.addEventListener(MouseEvent.MOUSE_UP, drop);
  spr_box.addEventListener(KeyboardEvent.KEY_DOWN, rotate);
  spr_box.addEventListener(KeyboardEvent.KEY_DOWN, unrotate);

  spr_box.width = 124;
  spr_box.height = 180;
  spr_box.x = 430;
  spr_box.y = 425;


  this.addChild(spr_box);
  loaded_images[img_name] = spr_box;
}


function drag(evt:MouseEvent):void
{
  evt.currentTarget.startDrag()
}

function drop(evt:MouseEvent):void
{
  evt.currentTarget.stopDrag()
}

function rotate(evt:KeyboardEvent):void
{
  if (evt.keyCode==Keyboard.D) {
    evt.currentTarget.rotation = 90 
  }
}

function unrotate(evt:KeyboardEvent):void
{
  if (evt.keyCode==Keyboard.A) {
    evt.currentTarget.rotation = 0 
  }
}

1 个答案:

答案 0 :(得分:2)

嗯 - 你只是在一个地方引用了道具A并且它抛出了一个未定义的错误。那么,要么你没有正确地链接Keyboard课......还是......?

无论如何 - 您还可以使用数字分配来捕获击键。在这种情况下:

function rotate(evt:KeyboardEvent):void
{
  if (evt.keyCode == 68) { //"D"
    evt.currentTarget.rotation = 90 
  }
}

function unrotate(evt:KeyboardEvent):void
{
  if (evt.keyCode == 65) {  //"A"
    evt.currentTarget.rotation = 0 
  }
}

它们都很清楚right here