Skip to content

Instantly share code, notes, and snippets.

@silviopaganini
Created September 10, 2012 17:13
Show Gist options
  • Save silviopaganini/3692221 to your computer and use it in GitHub Desktop.
Save silviopaganini/3692221 to your computer and use it in GitHub Desktop.
AS3 Workers
Main.as
function init() : void
{
// Set the Workers Manager the swf bytes
Workr.bytesMain = loaderInfo.bytes;
// initiate the singleton
workr = Workr.getInstance();
// create as many workers you want
workr.create(WorkerEnterFrame, "enterFrame");
// listen for the events
workr.get("enterFrame").listen(workerHandler);
// listen for the worker start
workr.get("enterFrame").ready = ready;
// start the workers
workr.start();
}
private function ready () : void
{
workr.get("enterFrame").send("startEnterFrame");
}
private function workerHandler(event : Event) : void
{
trace(workr.get("enterFrame").receive());
}
WorkerEnterFrame.as
// listen for any commands
override protected function handleCommandMessage(command : String) : void
{
switch(command)
{
case "startEnterFrame":
addEventListener(Event.ENTER_FRAME, ef);
break;
}
}
// execute your commands and send the info back to the main application
private function ef(event : Event) : void
{
counter++;
send(counter);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment