Processing to Flash AS3

This is my first attempt at getting processing to talk to flash and it works. It uses Actionscript 3.0 and grabs an XML stream from a server you setup in processing. This just handles two variables being passed to flash. I have them traced and you can see them in the output window. I made the values increase so that you are constantly receiving different values.

 import processing.net.*;
 
int port = 9001;
Server myServer;
//Variables for Sending
byte zero = 0;
int total = 299;
int total2 = 200;
 
void setup() {
  size (200, 200);
  myServer = new Server(this,port);
}
 
void draw () {
  total += 1;
  total2 +=2;
  myServer.write(total+","+total2);
  myServer.write(zero);
}

Flash Actionscript 3

// ===serverCom Class=== //
// ===     created by     ===//
// ===     Josh Shard     ===//
package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.DataEvent;
	import flash.events.IOErrorEvent;
	import flash.net.XMLSocket;
 
	public class serverCom extends Sprite {
 
		public var serialServer:XMLSocket;
 
		public function serverCom() {
			init();
		}
		public function init():void {
			serialServer=new XMLSocket  ;
			serialServer.connect("127.0.0.1",9001);
 
			serialServer.addEventListener(DataEvent.DATA,onReceiveData);
 
			serialServer.addEventListener(Event.CONNECT,onServer);
			serialServer.addEventListener(Event.CLOSE,onServer);
			serialServer.addEventListener(IOErrorEvent.IO_ERROR,onServer);
		}
		// --== EVENTS ==-- \\
		public function onServer(event:Event):void {
			trace(event);
		}
		public function onReceiveData(dataEvent:DataEvent):void {
 
			var Data:DataEvent=dataEvent;
			//trace(Data);
 
			// This grabs the data from Data var which is the string passed
			// from our processing server.
			var test=Data.data;
			//trace(test);
 
			// This splits the variables we are passing.
			var parts:Array=test.split(",");
			trace("parts0 this is the first variable: " + parts[0]);
			trace("parts1 this is the second variable: " + parts[1]);
		}
	}
}

One Comment

  1. Santiago Andrade
    Posted November 17, 2010 at 3:05 am | Permalink

    TY a lot you save my day :)

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*