-
Help in AS3 XMLSocket Class
I'm newbie to AS,and i need to create a class for this code,but i dont know how,i´ve tried many ways but with no luck ,can you give me a hand please!
Code:
import test;
var xc;
var yc;
var speed;
var parts;
var posx_txt;
var posy_txt;
var centerX;
var serialServer = new XMLSocket();
serialServer.connect("127.0.0.1",9001);
serialServer.onConnect = function(success)
{
trace("the client connected to the server");
}
serialServer.onData = gotSomething;
trace(gotSomething);
function gotSomething(datar)
{
parts = datar.split(",");
xc = parts[0];
yc = parts[1];
trace(parts);
posx_txt.text = xc;
posy_txt.text = yc;
xc=320-xc;
xc=xc*800/320;
speed = (xc-centerX)/9500;
trace("xc:"+xc);
}
Tks in Advance!
Last edited by firestruck; 07-12-2008 at 01:33 PM.
-
This would do roughly what you've got there...although you never set centerX anywhere? Also this is a pretty rigid implementation...you might want to move the host and port to variables that you set with the constructor function.
PHP Code:
package {
/*
* Flash 9.0 :: ActionScript 3.0
*/
//--------------------------------------
// PACKAGES
//--------------------------------------
import flash.events.*;
import flash.net.XMLSocket;
public final class SerialServerSocket {
//--------------------------------------
// CLASS CONSTANTS
//--------------------------------------
private const hostName:String = "127.0.0.1";
private const port:int = 9001;
private const serialServer:XMLSocket = new XMLSocket();
//--------------------------------------
// CONSTRUCTOR
//--------------------------------------
public function SerialServerSocket(){
super();
initSocket();
}
//--------------------------------------
// PRIVATE VARIABLES
//--------------------------------------
private var centerX:Number = 0;
//--------------------------------------
// GETTER/SETTERS
//--------------------------------------
//--------------------------------------
// PRIVATE & PROTECTED INSTANCE METHODS
//--------------------------------------
private function initSocket():void{
serialServer.addEventListener(Event.CONNECT, onSocketConnect);
serialServer.addEventListener(DataEvent.DATA, onDataReceived);
serialServer.addEventListener(IOErrorEvent.PROGRESS, onIOError);
serialServer.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
serialServer.connect(hostName, port);
}
//--------------------------------------
// EVENT HANDLERS
//--------------------------------------
private function onSocketConnect(e:Event):void{
trace("the client connected to the server");
}
private function onDataReceived(e:DataEvent):void{
var parts:Array = e.data.split(',');
var xc:Number = parts[0] as Number;
var yc:Number = parts[1] as Number;
trace(parts);
posx_txt.text = xc as String;
posy_txt.text = yc as String;
xc = 320 - xc;
xc *= 800/320;
speed = (xc - centerX) / 9500;
trace('xc:', xc);
}
private function onIOError(e:IOErrorEvent):void{
trace('IO Error', e);
}
private function onSecurityError(e:SecurityErrorEvent):void{
trace('Security Error', e)
}
}
}
Save that as SerialServerSocket.as in the same folder as your fla and then you can initialize it with
PHP Code:
var sss:SerialServerSocket = new SerialServerSocket();
-
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|