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

SAE Magazin 2/2011

Hendrik Maus (aka Aiden Tailor) has been working for quite a time as a freelance Web-Developer and Tutor. Since 2011 he is a Head Instructor at SAE Institute Cologne. http://aidentailor.net Unsere Klasse AlarmClock erstellt eine Objektinstanz des Sig- nals im Konstruktor und liefert das Signal aus sobald die ring() Methode von Außen aufgerufen wird. Wollen wir unsere Klasse Alarmglocke nun benutzen, erstel- len wir zuerst einmal eine Objektinstanz und registrieren uns für das erwartete Signal auf der öffentlichen Eigenschaft alarm mit der add() Methode. Wird das Signal ausgeliefert, so wird die Methode onRing() ausgeführt. Denken wir an native Flash Events haben wir grade einen Listender und dessen Händler erstellt. Zu guter Letzt rufen wir die ring() Methode der Alarm- glocke Instanz auf. Unsere Flash Applikation zeigt ‚Wake up!‘ in der Konsole an. Argumente übergeben Wollen wir Argumente mit unserem Signal transportieren, so übergeben wir deren Datentypen dem Konstruktor der Signal Klasse und übergeben jene Argumente in gleicher Reihenfolge bei der Auslieferung des Signals. Mehrere Argumente trennen wir wie gewohnt mit dem Komma. Natürlich muss unser Signal- Handler jene Argumente auch entgegennehmen. Ansonsten entdeckt der Compiler einen Fehler im Quelltext. Tipp: Übergibt man mehrere Parameter so kann man diese komfortabel in einem anonymen Objekt unterbringen. Hinzufügen und Entfernen der Signal-Handler MankommtoftindieSituation,daseinEventnachdemesgenau einmal abgefeuert wurde direkt gelöscht werden kann, Signals stellt hierzu die addOnce() Methode zur Verfügung. Wird mit- tels dieser Methode ein Listender registriert, so wird er direkt wieder gelöscht sobald das Signal einmal ausgeliefert wurde. Our AlarmClock class creates a public property to hold the alarm signal, instantiates the signal in the constructor and dis- patches it whenever the public method ring() is called. To make use of our AlarmClock we first of all instantiate it. Af- ter that we use the add() method on the public property alarm, which is the signal, to add a listener to it called onRing. Last but not least we want to make sure that the AlarmClock actu- ally rings so we call the ring() method on it. If you run the application, you’d see ‘Wake up!’in the console. Delivering Arguments All we have to do in order to deliver one or more arguments with our signal is to define the type in the Signalsʼ constructor and dispatching whatever we want in the dispatch() method. Separate multiple arguments with a comma. The handler will need to take the arguments, otherwise the compiler will throw an error. Hint: If you pass more than two arguments, make use of an anonymous object instead. Adding and Removing Signals You often find a situation where you want an event to only fire once and then be immediately destroyed. In comparison to na- tive events, signals provide a convenient way to do this. So letʼs modify our WakeUp class to only catch the signal once by replacing the add() method with the addOnce() method. # Signals. The main advantages of the framework are speed and less code. What is a Signal? oncept of Signals is to not use string-base channels, and to let the Signal be a concrete member of a clas Production & Know How // WEB // AS3 Signals 113