I've seen a lot of tutorials on this and none of them seem so straightforward so here goes. So let's say i have a class and i have another class that extends something like socket and i want to know when something happens in my socket class, like an event…
i'll have to create a custom event class:
package { import flash.events.Event public class ServerEvent extends Event { public static const ON_LOGIN:String = "onLogin" public function ServerEvent(type:String):void { super(type) } } }
so then in my class which extends Socket, I will simply dispatch this event when I want it to fire:
package { import flash.events.* import flash.net.Socket public class CustomSocket extends Socket { //custom class goes here //run onLogin function when logged in } public function onLogin():void{ dispatchEvent(new ServerEvent("onLogin")) } }
so then, when we instantiate our custom server class in our pre we simply add an event listener like we normally would.
package { import flash.display.Sprite public class Sockets extends Sprite { public var socket:CustomSocket public function Sockets() { socket = new CustomSocket() socket.addEventListener(ServerEvent.ON_LOGIN, loggedIn) } public function loggedIn(e:ServerEvent):void{ trace("yay! custom event worked!") } } }
seems easy right? awesome.