开发者

Actionscript 3.0 Keyboard event listener doesn't call on function or even fire

The keyBoard event listeners call on the rotate and unrotate functions and based on the key inputs(A and D) will implement either of the functions and rotate the image or return it to the original upright position. However, I haven't been able to get the function to work. When I press the A or D keys on the keyboard nothing happens. I even put trace() in one of the functions to see if the function will even be implemented but I don't get anything. I put in my Scripts below. Why are the functions not working? Do the eventlisteners fire or are there conflicts? I'm not getting any error messages.

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==68) {
    evt.currentTarget.rotation = 90 }
}

function unrotate(keyEv开发者_开发百科ent:KeyboardEvent):void
{
    if (evt.keyCode==65) {
    evt.currentTarget.rotation = 0; 
    trace("A key pressed")}
}


Because sprites don't necessarily dispatch keyboard events. Add the listener to the stage instead

stage.addEventListener(KeyboardEvent.KEY_DOWN, rotate);
stage.addEventListener(KeyboardEvent.KEY_DOWN, unrotate);

You'll need to keep track of which "spr_box" is selected, I'm guessing you'll do that with a double click. Create a variable called currentSpr_box, and then in your listener function, set the currentSpr_box as the currentTarget

spr_box.addEventListener(MoustEvent.DOUBLE_CLICK, onSelect); //add this when creating spr_box in onLoadingComplete

private function onSelect(event:MouseEvent):void{
   currentSpr_box = event.currentTarget as Sprite;
}

In your rotate functions, you need do set currentSpr_box.rotation

Update... full code sample

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");
    }

stage.addEventListener(KeyboardEvent.KEY_DOWN, rotate);
stage.addEventListener(KeyboardEvent.KEY_DOWN, unrotate);

var loaded_images:Dictionary = new Dictionary();
var currentSpr_Box:Sprite;

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(MouseEvent.DOUBLE_CLICK, onSelect);

    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 onSelect(evt:MouseEvent):void{
    currentSpr_box = event.currentTarget as Sprite;
}

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

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

function rotate(evt:KeyboardEvent):void
{
    if (currentSpr_box != null && evt.keyCode==68)  currentSpr_box.rotation = 90;
}

function unrotate(keyEvent:KeyboardEvent):void
{
    if (currentSpr_box != null && evt.keyCode==65) currentSpr_box.rotation = 0; 
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜