Reputation: 51
I am trying to show the first image of my image gallery when it is loaded. How would I go about simulating a mouse click event that will pass an event value to a function?
Here is my code:
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;
}
Upvotes: 1
Views: 2501
Reputation: 4434
if you modify your code like this:
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;
}
you'll have just to call displayImg();
to init your gallery
Upvotes: 2
Reputation: 2279
It's also common practice to make the event object argument optional and implement default behavior, like so (note that int
would be the better number type to use here as you're accessing an array index):
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;
}
Upvotes: 4
Reputation: 13151
You don't really have to. Just move the displayImg code to a function with a string argument. Now it will be callable without the mouse event. Something like:
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;
}
Upvotes: 4
Reputation: 22604
You could of course manually dispatch a MouseEvent.CLICK on the image:
myContainer.dispatchEvent (new MouseEvent(MouseEvent.CLICK));
Beware, though, automation of user events often leads to glitches in the program: What if the user clicks somewhere else first, maybe you want to have other listeners attached, etc.
I find it more useful to separate functions: An event handler should contain only handling of the event, displayImg would become a separate method.
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;
}
You can then call displayImg ("1");
instead of dispatching an event.
Upvotes: 3