我正在尝试在加载时显示我的图片库的第一张图片。 我将如何模拟将事件值传递给函数的鼠标单击事件?
这是我的代码:
private function createThumbContainer(my_X:Number, my_Y:Number):void{
ThumbsContainerMC = new MovieClip();
ThumbsContainerMC.buttonMode = true;
ThumbsContainerMC.x = my_X;
ThumbsContainerMC.y = my_Y;
addChild(ThumbsContainerMC);
// Here I am trying to enter displayImg() with the value of 0 in my evt.target.name
//ThumbsContainerMC.getChildByName("name").dispatchEvent(new Event(MouseEvent.CLICK));
ThumbsContainerMC.addEventListener(MouseEvent.CLICK, displayImg);
}
private function displayImg(evt:MouseEvent):void {
var imgURL = imgList[Number(evt.target.name)];
imgTitle.text = imgURL.@label;
if (evt.target.name != activeImg) {
imgLoader.load(new URLRequest(imgURL));
}
activeImg = evt.target.name;
}
答案 0 :(得分:4)
你真的不需要。只需将displayImg代码移动到带有字符串参数的函数即可。现在它可以在没有鼠标事件的情况下调用。类似的东西:
private function createThumbContainer(my_X:Number, my_Y:Number):void{
ThumbsContainerMC = new MovieClip();
ThumbsContainerMC.buttonMode = true;
ThumbsContainerMC.x = my_X;
ThumbsContainerMC.y = my_Y;
addChild(ThumbsContainerMC);
// Here I am trying to enter displayImg() with the value of 0 in my evt.target.name
loadImg("0");
ThumbsContainerMC.addEventListener(MouseEvent.CLICK, displayImg);
}
private function Img_Clicked(evt:MouseEvent):void {
loadImg(evt.target.name);
}
private function loadImg(String targetName) {
var imgURL = imgList[Number(targetName)];
imgTitle.text = imgURL.@label;
if (targetName != activeImg) {
imgLoader.load(new URLRequest(imgURL));
}
activeImg = targetName;
}
答案 1 :(得分:4)
通常的做法是使事件对象参数可选并实现默认行为,如下所示(注意int
将是您在访问数组索引时使用的更好的数字类型):
private function createThumbContainer(my_X:Number, my_Y:Number):void{
ThumbsContainerMC = new MovieClip();
ThumbsContainerMC.buttonMode = true;
ThumbsContainerMC.x = my_X;
ThumbsContainerMC.y = my_Y;
addChild(ThumbsContainerMC);
// Here I am trying to enter displayImg() with the value of 0 in my evt.target.name
//ThumbsContainerMC.getChildByName("name").dispatchEvent(new Event(MouseEvent.CLICK));
displayImg();
ThumbsContainerMC.addEventListener(MouseEvent.CLICK, displayImg);
}
private function displayImg(evt:MouseEvent = null):void {
var imgIndex:int = evt ? int(evt.target.name) : 0;
var imgURL = imgList[imgIndex];
imgTitle.text = imgURL.@label;
if (imgIndex != activeImg) {
imgLoader.load(new URLRequest(imgURL));
}
activeImg = imgIndex;
}
答案 2 :(得分:3)
您当然可以在图像上手动调度MouseEvent.CLICK:
myContainer.dispatchEvent (new MouseEvent(MouseEvent.CLICK));
请注意,用户事件的自动化通常会导致程序出现问题:如果用户首先点击其他地方,或许您想要连接其他听众,等等。
我觉得分离函数更有用:事件处理程序应该只包含对事件的处理,displayImg将成为一个单独的方法。
private function onThumbClick (ev:MouseEvent) : void {
var name:String = evt.target.name;
displayImg ( name );
}
private function displayImg ( name:String ) : void {
var imgURL = imgList[parseInt (name)];
imgTitle.text = imgURL.@label;
if (name != activeImg) {
imgLoader.load(new URLRequest(imgURL));
}
activeImg = name;
}
然后,您可以拨打displayImg ("1");
而不是发送活动。
答案 3 :(得分:2)
如果您修改代码如下:
private function displayImg(evt:MouseEvent = null):void {
var name: String = evt ? evt.target.name : '0';
var imgURL = imgList[Number(name)];
imgTitle.text = imgURL.@label;
if (name != activeImg) {
imgLoader.load(new URLRequest(imgURL));
}
activeImg = name;
}
您只需致电displayImg();
来启动您的图库