How do you pass parameters / variables through event listeners? I\'ve overcome this problem fairly effectively using anonymous functions; which is an incredibly simple solut
If the MovieClip needs to keep track of extra information that you need to know when it's been clicked, then I would use a more formalised version of your Leprechaun technique.
I'd create a new class called something like SpecialImage that extends MovieClip and has a property for storing the url to load.
Depending on how your SpecialImages are created you can either create them in code or link a MovieClip in your library to this class.
It's basically a posh leprechaun, but as the information is directly related to the image MovieClip, storing them together feels like the best thing.
Anonymous functions are better avoided as they can make the code clumsy and sometimes lead to memory leaks. I would use custom events if I am responsible for dispatching the event too, but that is not the case here.
In this case, I would store the URLs and movie clips in two separate class level arrays and use
var url:String = urlArray[mcArray.indexOf(event.target)];
inside the event handler to get the URL.
Not that there is anything wrong with storing the data in the movieclip, but I don't feel that it's a good way to go. But following best practices even when they make things difficult is not a good idea either - the choice is yours.
Yes I know what you mean. What I do is to extend the event and so dispatch a custom event. This way I can create properties in the custom event that contain the necessary data. It seems clean but does require a bit of effort. It is not ideal but I don't know of a significantly better way.
You can use what's called "Delegates" if you don't like storing the data in the target member. Neither is better than the other. It's mostly just preference.
Here's an example of a Delegate class (nothing built-in): http://www.actionscript.org/resources/articles/205/1/The-Delegate-Class/Page1.html
That's probably the best idea if the eventdispatcher isn't your own class. Otherwise, storing the variable in your own class isn't a bad idea at all. Just OOP style.
Cheers.
It is probably better practice to make a custom class that extends MovieClip for your thumnbnail, and have that thumbnail dispatch a custom Event class that has a place to store your data:
Make a custom URLEvent
that can store the url
property along with the event
package{
import flash.events.Event;
public class URLEvent extends Event{
public var url:String;
public static const CLICK:String = "URLEventClick"
public function URLEvent(type:String, url:String = "", bubbles:Boolean = false, cancelable:Boolean=false){
super(type, bubbles, cancelable);
this.url = url;
}
override public function clone():Event{
return new URLEvent(type,url,bubbles,cancelable);
}
}
}
Make a custom thumbnail class that can store the url
variable, and sends a URLEvent when clicked:
package{
import flash.display.MovieClip;
import flash.events.MouseEvent;
public class Thumbnail extends MovieClip{
protected var url:String;
public function Thumbnail(url:String){
this.url = url;
addEventListener(MouseEvent.CLICK,clickHandler);
}
//the thumbnail captures its own click and dispatches a custom event
protected function clickHandler(event:MouseEvent):void{
dispatchEvent(new URLEvent(URLEvent.CLICK,url));
}
}
Now you can use your Thumbnail
class to load each image:
tempThumb.addEventListener(URLEvent.CLICK,tempThumbClickHandler);
function tempThumbClickHandler(event:URLEvent):void{
//we can get the url property direct from the URLEvent
loadImage(event.url);
}
This method is much better OOP practice, as you do not need to know what type of object your event.target is - you know from the event type exactly what type of data will be sent with each event.
If I were looking for this functionality, and I often do, I would make tempThumb a Class that carried the url property. then:
MyThumbClass(event.target).url //in the MouseEvent.CLICK handler
Strong typing, easy access, clean syntax, low complexity.