A Flash Developer Resource Site

Results 1 to 3 of 3

Thread: Help in AS3 Class

  1. #1
    Junior Member
    Join Date
    Jul 2008
    Posts
    3

    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.

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    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.CONNECTonSocketConnect);
                    
    serialServer.addEventListener(DataEvent.DATAonDataReceived);
                    
    serialServer.addEventListener(IOErrorEvent.PROGRESSonIOError);
                    
    serialServer.addEventListener(SecurityErrorEvent.SECURITY_ERRORonSecurityError);
                    
    serialServer.connect(hostNameport);
                }
                
              
    //--------------------------------------
              //  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(); 

  3. #3
    Junior Member
    Join Date
    Jul 2008
    Posts
    3
    Tks for all!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center