0

您好,我正在尝试在 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.

我必须进口一些东西吗???有任何想法吗 ?

4

1 回答 1

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
        }
    }

}
于 2012-10-02T15:25:06.540 回答