keyBoard事件侦听器调用旋转和非旋转函数,并根据键输入(A和D)实现任一功能并旋转图像或将其返回到原始直立位置。但是,我无法使该功能正常工作。当我按下键盘上的A或D键时没有任何反应。我甚至将trace()放在其中一个函数中,看看函数是否会被实现,但我什么也得不到。我在下面输入了我的脚本。为什么功能不起作用? eventlisteners会解雇还是有冲突?我没有收到任何错误消息。
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(keyEvent:KeyboardEvent):void
{
if (evt.keyCode==65) {
evt.currentTarget.rotation = 0;
trace("A key pressed")}
}
答案 0 :(得分:4)
因为精灵不一定会调度键盘事件。将侦听器添加到舞台
stage.addEventListener(KeyboardEvent.KEY_DOWN, rotate);
stage.addEventListener(KeyboardEvent.KEY_DOWN, unrotate);
你需要跟踪选择了哪个“spr_box”,我猜你会通过双击来实现。创建一个名为currentSpr_box
的变量,然后在您的侦听器函数中,将currentSpr_box
设置为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;
}
在旋转功能中,您需要设置currentSpr_box.rotation
更新...完整代码示例
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;
}