我该如何解决我的问题?
当我单击按钮时,我会加载所有图像和图像按钮,但是此后,当我尝试单击关闭按钮(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范围之外声明变量。它们似乎更具有全局变量的意义,而不仅仅是局部变量(因为您尝试使用在test方法的onShop方法上声明的相同变量)。
我还建议您注意以下几点:
for
循环加载所有图像(遵循之前的建议);[event.currentTarget]正在使用事件侦听器主动处理Event对象的对象。例如,如果用户单击“确定”按钮,则当前目标可能是包含该按钮的节点或其已为该事件注册了事件侦听器的祖先之一。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句