事件侦听器未被调用
event listener not being called
我该如何解决我的问题?
我在单击按钮时加载了所有图像和图像按钮,但之后当我尝试单击关闭按钮 (image4) 时,事件侦听器无法侦听,因为所有内容都在另一个函数中设置。
private function onShop(evt:MouseEvent)
{
shop.removeEventListener(MouseEvent.CLICK, onShop);
var container:Sprite = new Sprite();
var ibutton:Sprite = new Sprite();
addChild(container);
addChild(ibutton);
ibutton.buttonMode = true;
function loadImage(path:String):Loader
{
var request:URLRequest = new URLRequest(path);
var loader:Loader = new Loader();
loader.load(request);
return loader;
};
var image1 = loadImage("image/inventory/shop_windoww.png");
image1.x = 200;
image1.y = 40;
this.addChildAt(image1,8);
var image2 = loadImage("image/inventory/title_window.png");
image2.x = 200;
image2.y = 14;
container.addChild(image2);
var image3 = loadImage("image/inventory/icon_window.png");
image3.x = 177;
image3.y = 7;
container.addChild(image3);
var image4 = loadImage("image/inventory/close.png");
image4.x = 891;
image4.y = 39;
ibutton.addChild(image4);
var image5 = loadImage("image/inventory/button_ok.png");
image5.x = 450;
image5.y = 485;
ibutton.addChild(image5);
var image6 = loadImage("image/inventory/button_ok.png");
image6.x = 782;
image6.y = 485;
ibutton.addChild(image6);
image4.addEventListener(MouseEvent.CLICK, test);
}
private function test(evt:MouseEvent)
{
image4.removeEventListener(MouseEvent.CLICK, test);
trace("ca marche");
}
在 onShop 范围之外声明您的变量。它们似乎作为全局变量更有意义,而不仅仅是局部变量(因为您正试图在测试方法上使用在 onShop 方法上声明的相同变量)。
我也推荐你几点:
- 使用外部文件 (JSON/XML) 获取所有图像 URL,或者只使用带有硬编码内容的简单数组;
- 使用
for
循环加载所有图像(按照之前的建议);
- 在你的测试方法中使用event.currentTarget,这样你就可以用更通用的方式处理这些图像。
[event.currentTarget] 正在使用事件侦听器主动处理 Event 对象的对象。例如,如果用户单击“确定”按钮,则当前目标可能是包含该按钮的节点或其已为该事件注册事件侦听器的祖先之一。
我该如何解决我的问题?
我在单击按钮时加载了所有图像和图像按钮,但之后当我尝试单击关闭按钮 (image4) 时,事件侦听器无法侦听,因为所有内容都在另一个函数中设置。
private function onShop(evt:MouseEvent)
{
shop.removeEventListener(MouseEvent.CLICK, onShop);
var container:Sprite = new Sprite();
var ibutton:Sprite = new Sprite();
addChild(container);
addChild(ibutton);
ibutton.buttonMode = true;
function loadImage(path:String):Loader
{
var request:URLRequest = new URLRequest(path);
var loader:Loader = new Loader();
loader.load(request);
return loader;
};
var image1 = loadImage("image/inventory/shop_windoww.png");
image1.x = 200;
image1.y = 40;
this.addChildAt(image1,8);
var image2 = loadImage("image/inventory/title_window.png");
image2.x = 200;
image2.y = 14;
container.addChild(image2);
var image3 = loadImage("image/inventory/icon_window.png");
image3.x = 177;
image3.y = 7;
container.addChild(image3);
var image4 = loadImage("image/inventory/close.png");
image4.x = 891;
image4.y = 39;
ibutton.addChild(image4);
var image5 = loadImage("image/inventory/button_ok.png");
image5.x = 450;
image5.y = 485;
ibutton.addChild(image5);
var image6 = loadImage("image/inventory/button_ok.png");
image6.x = 782;
image6.y = 485;
ibutton.addChild(image6);
image4.addEventListener(MouseEvent.CLICK, test);
}
private function test(evt:MouseEvent)
{
image4.removeEventListener(MouseEvent.CLICK, test);
trace("ca marche");
}
在 onShop 范围之外声明您的变量。它们似乎作为全局变量更有意义,而不仅仅是局部变量(因为您正试图在测试方法上使用在 onShop 方法上声明的相同变量)。
我也推荐你几点:
- 使用外部文件 (JSON/XML) 获取所有图像 URL,或者只使用带有硬编码内容的简单数组;
- 使用
for
循环加载所有图像(按照之前的建议); - 在你的测试方法中使用event.currentTarget,这样你就可以用更通用的方式处理这些图像。
[event.currentTarget] 正在使用事件侦听器主动处理 Event 对象的对象。例如,如果用户单击“确定”按钮,则当前目标可能是包含该按钮的节点或其已为该事件注册事件侦听器的祖先之一。