2013-06-03 57 views
1

我在Actionscript 3中用於控制Flash,但我認爲問題是一個通用對象。將文本字段的引用傳遞給對象

我在AS3中創建了一個TextField,並將其顯示在SocketExample類的屏幕上。

(目前它只是說「你好」)

此類創建一個用於讀取從在端口7001的外部IRC服務器發送字符串,我想這些字符串顯示爲文本的套接字對象在文本字段中,但由於某種原因,套接字類不認爲refTextField存在。

AS3的Adobe Flash代碼:

package { 
    import flash.display.Sprite; 
    import flash.text.*; 

    public class SocketExample extends Sprite { 
     private var socket:CustomSocket; 

     public function SocketExample() { 

      var myTextField:TextField = new TextField(); 
      myTextField.text = "Hello"; 
      addChild(myTextField); 
      socket = new CustomSocket("localhost", 7001, myTextField); 

     } 
    } 
} 

import flash.errors.*; 
import flash.events.*; 
import flash.net.Socket; 
import flash.text.TextField; 

class CustomSocket extends Socket { 
    private var response:String; 

    public function CustomSocket(host:String = null, port:uint = 0, refTextField:TextField = null) { 
     super(); 
     configureListeners(); 
     if (host && port) { 
      super.connect(host, port); 
     } 
    } 

    private function configureListeners():void { 
     addEventListener(Event.CLOSE, closeHandler); 
     addEventListener(Event.CONNECT, connectHandler); 
     addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler); 
     addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler); 
     addEventListener(ProgressEvent.SOCKET_DATA, socketDataHandler); 
    } 

    private function writeln(str:String):void { 
     str += "\n"; 
     try { 
      writeUTFBytes(str); 
     } 
     catch(e:IOError) { 
      trace(e); 
     } 
    } 

    private function sendRequest():void { 
     trace("sendRequest"); 
     response = ""; 
     writeln("This String was sent from Flash."); 
     flush(); 
    } 

    private function readResponse():void { 
     var str:String = readUTFBytes(bytesAvailable); 
     response += str; 
     trace(str); 
     refTextField.text = str; 
    } 

    private function closeHandler(event:Event):void { 
     trace("closeHandler: " + event); 
     trace(response.toString()); 
    } 

    private function connectHandler(event:Event):void { 
     trace("connectHandler: " + event); 
     sendRequest(); 
    } 

    private function ioErrorHandler(event:IOErrorEvent):void { 
     trace("ioErrorHandler: " + event); 
    } 

    private function securityErrorHandler(event:SecurityErrorEvent):void { 
     trace("securityErrorHandler: " + event); 
    } 

    private function socketDataHandler(event:ProgressEvent):void { 
     trace("socketDataHandler: " + event); 
     readResponse(); 
    } 
} 

的AS3程序需要一臺服務器,沒有運行任何端口7001,只是爲了完整性這裏是一個可行的Python腳本回聲服務器。

Python代碼

# Echo server program 
import socket 
import sys 

HOST = None    # Symbolic name meaning all available interfaces 
PORT = 7001    # Arbitrary non-privileged port 
s = None 

def startListening(): 
    print "starting to listen" 

    for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC, 
            socket.SOCK_STREAM, 0, socket.AI_PASSIVE): 
     af, socktype, proto, canonname, sa = res 
     try: 
      s = socket.socket(af, socktype, proto) 
     except socket.error as msg: 
      s = None 
      continue 
     try: 
      s.bind(sa) 
      s.listen(1) 
     except socket.error as msg: 
      s.close() 
      s = None 
      continue 
     break 
    if s is None: 
     print 'could not open socket' 
     sys.exit(1) 
    conn, addr = s.accept() 
    print 'Connected by', addr 
    while 1: 
     data = conn.recv(1024) 
     if not data: 
      break 
     print data 
     conn.send("I got that, over!") 

    conn.close() 
    print "connection closed" 

while 1: 
    startListening() 

回答

1

在此行之後CustomSocket類:

private var response:String;

加入這一行:

private var refTextField:TextField;

而且在此行之後0構造:

​​

加入這一行:

this.refTextField = refTextField;

這樣您CustomSocket實例將保持您TextField實例的引用。

+0

謝謝你的工作! – Zac

+0

@Zac太棒了!很高興幫助! – Cherniv