Please activate JavaScript!
Please install Adobe Flash Player, click here for download

SAE Magazin 2/2011

Mit den Methoden remove() und remo- veAll() ist es möglich Listender manu- ell zu entfernen. Das DeluxeSignal & GenericEvent Mit der DeluxeSignal Klasse können wir ein Signal erzeugen welches alle Informationen trägt die ein herkömmliches Flash Event auch tragen würde. Wir können also eine Referenz zum Ziel und auch zum Signal selbst erhalten. Zur Nutzung dieser Art von Signal passen wir unsere Alarmglocke Klasse wie folgt an: Unsere Eigenschaft alarm wird als DeluxeSignal typisiert und im Konstruktor installiert. Hierbei wird das Schlüssel- wort übergeben. Nun kennt das Signal die Objektinstanz der Alarmglocke Klasse. Mit der öffentlichen Eigenschaft message wollen wir eine Botschaft transportieren die im Signal-Listener abgefragt werden kann. Beim ausliefern des Signals übergeben wir nun eine anonyme Instanz der GenericEvent Klasse. Diese Objektinstanz trägt alle nötigen Informationen über den Vorgang. Natürlich muss unserer Signal-Listener nun ein Argument vom Typ GenericEvent entgegennehmen, wie es bei der Methodik eines Flash Event auch der Fall wäre. Fazit AS3 Signals macht das Entwicklerleben einfacher, schneller und effizienter. ■ This way the listener will be executed once and automatically be removed afterwards. To achieve this we could either make use of the removeAll() method. DeluxeSignal & GenericEvent How can we create a signal carrying all the data a native event would? A native event, for example, tells us everything about the target it came from. A DeluxeSignal enables us to get access to the target and to the signal itself. So we are now able to manipulate both of them. Of course the DeluxeSignal comes with all the capabilities of a basic signal. We need to modify the AlarmClock class like this: We create a public property for our DeluxeSignal and instanti- ate it in the constructor passing this to the DeluxeSignal con- structor. Now the DeluxeSignal knows about the AlarmClock. We want to add another public property, which can be accessed by the listener later, called message. The DeluxeSignal is be- ing dispatched as we are used to, but this time weʼll pass a new instance of a GenericEvent to it, which will carry all the infor- mation about the class the signal is coming from and about the signal itself. The listener now takes an argument of type GenericEvent, which we passed over to it in the AlarmClock class. Now we can trace our target and other arguments like our message. The one thing special about this is that we are also able to access the signal which was dispatched from the AlarmClock. Conclusion Signals can make a developer’s life a whole lot easier by just being fast, efficient and easy to use. ■ What is AS3 Signals? Basically AS3 Signals is a communication framework much like the native events, but its concept is inpired by C# channels, and to let the Signal be a concrete member of a class. An overview of AS3 Signals resources The project is being hosted on GitHub https://github.com/robertpenner/as3-signals The project page on Github features a wiki with multiple resources and other stuff Infobox 114 Production & Know How // WEB // AS3 Signals