Reputation: 25
I have this keyboard event listener to listen for key_Down of letter A and D but I end up with and error:
1119: Access of possibly undefined property A through a reference with static type Class.
When I use
function rotate(evt:KeyboardEvent):void
{
if (evt.keyCode==68) {
evt.currentTarget.rotation = 90 }
}
function unrotate(evt:KeyboardEvent):void
{
if (evt.keyCode==65) {
evt.currentTarget.rotation = 0 }
}
instead of (keyBoard.A and keyboard.D, I no longer get and error but the function that rotates the images doesn't work. Is it that it can't recognize the current target and implement the function or something else?
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
}
}
Upvotes: 0
Views: 516
Reputation: 61
When you click on the object, it select its current Target. But when you press key code, how can it selects its target? You must define its instance name when you press a key code.
Upvotes: 0
Reputation: 4595
or even better using Fingers
on(stage).keyDown += rotate + unrotate;
Have in mind, that you have to change evt.currentTarget
in your listeners, so it points to an instance of DisplayObject
you want to rotate.
What's more, it would be clearer to have one stage_keyDownHandler
instead of rotate
and unrotate
:
stage_keyDownHandler(evt:KeyboardEvent):void
{
if (evt.keyCode == Keyboard.D) {
yourObjToRotate.rotation = 90
}
else if (evt.keyCode == Keyboard.A) {
yourObjToRotate.rotation = 0
}
}
Upvotes: 0
Reputation: 12281
You need to attach the KeyBoardEvent
to the stage not an object
stage.addEventListener(KeyboardEvent.KEY_DOWN, rotate);
stage.addEventListener(KeyboardEvent.KEY_DOWN, unrotate);
Upvotes: 1