您好我正在尝试在flash中打开套接字。所以我按照一个教程但是我遇到了错误:
package com.game.game
{
import flash.net.socket;
import flash.events.*;
public dynamic class game
{
var mysocket:Socket = new Socket();
Security.allowDomain("*");
mysocket.addEventListener(Event.CONNECT, onConnect);
mysocket.addEventListener(Event.CLOSE, onClose);
mysocket.addEventListener(IOErrorEvent.IO_ERROR, onError);
mysocket.addEventListener(ProgressEvent.SOCKET_DATA, onResponse);
mysocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError);
mysocket.connect("hejp.co.uk", 80);
}
}
我收到了这些错误:
1120: Access of undefined property mysocket.
1120: Access of undefined property onConnect.
1120: Access of undefined property mysocket.
1120: Access of undefined property onClose.
1120: Access of undefined property mysocket.
1120: Access of undefined property onError.
1120: Access of undefined property mysocket.
1120: Access of undefined property onResponse.
1120: Access of undefined property mysocket.
1120: Access of undefined property onSecError.
1120: Access of undefined property mysocket.
The class 'com.game.game.game' must subclass 'flash.display.MovieClip' since it is linked to a library symbol of that type.
我必须导入一些东西吗? 有什么想法吗?
答案 0 :(得分:1)
看起来你有正确的套接字代码,但它需要在方法内部。如果您将代码放在构造函数方法中实例化套接字,那么在实例化类时将连接到套接字。或者,您可以将套接字代码放在可以从类外部调用的不同公共方法中。
您可能还需要通过在声明中声明公共或私有来说明您的类属性和方法的范围。
您还需要声明每个侦听器函数,否则套接字将不具有要连接的函数。
package com.game.game
{
import flash.net.socket;
import flash.events.*;
public dynamic class game
{
//public class variables
public var mysocket:Socket;
//constructor
public function game() {
mysocket = new Socket();
Security.allowDomain("*");
mysocket.addEventListener(Event.CONNECT, onConnect);
mysocket.addEventListener(Event.CLOSE, onClose);
mysocket.addEventListener(IOErrorEvent.IO_ERROR, onError);
mysocket.addEventListener(ProgressEvent.SOCKET_DATA, onResponse);
mysocket.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecError);
mysocket.connect("hejp.co.uk", 80);
}
//private listener methods
private function onConnect(evt:Event):void {
//connect method code
}
private function onClose(evt:Event):void {
//close method code
}
private function onError(evt:IOErrorEvent):void {
//error method code
}
private function onResponse(evt:ProgressEvent):void {
//response method code
}
private function onSecError(evt:SecurityErrorEvent):void {
//security error method code
}
}
}