Twisted4000
Twisted4000

Reputation: 1

AddEventListener from Package in AIR Error #1067?

I'm publishing a Flash game I'm making in AIR 2.6 rather than Flash Player, since that's the only way I can get it to work with TCP. For some reason though, doing this will not allow me to import packages, seemingly...

If I have it published through Flash Player, this code works fine:

import TCP;
addEventListener(Event.ENTER_FRAME, TCP);

When I do the same exact thing but publish with AIR 2.6, I get this error:

1067: Implicit coercion of a value of type Class to an unrelated type Function.

This is the entire package:

package
{ 
    import flash.display.Sprite; 
    import flash.events.EventDispatcher;
    import flash.events.Event; 
    import flash.events.*;
    import flash.events.IOErrorEvent; 
    import flash.events.ProgressEvent; 
    import flash.events.ServerSocketConnectEvent; 
    import flash.net.ServerSocket; 
    import flash.net.Socket;

    public class TCP extends Sprite 
    { 
        private var serverSocket:ServerSocket; 
        private var clientSockets:Array = new Array(); 

        public function TCP()
        { 
            try 
            { 
                // Create the server socket 
                serverSocket = new ServerSocket(); 

                // Add the event listener 
                serverSocket.addEventListener( Event.CONNECT, connectHandler ); 
                serverSocket.addEventListener( Event.CLOSE, onClose ); 

                // Bind to local port 8087 
                serverSocket.bind( 8087, "127.0.0.1" ); 

                // Listen for connections 
                serverSocket.listen(); 
                trace( "Listening on " + serverSocket.localPort ); 

            } 
            catch(e:SecurityError) 
            { 
                trace(e); 
            } 
        } 

        public function connectHandler(event:ServerSocketConnectEvent):void 
        { 
            //Thesocket is provided by the event object 
            var socket:Socket = event.socket as Socket; 
            clientSockets.push( socket ); 

            socket.addEventListener( ProgressEvent.SOCKET_DATA, socketDataHandler); 
            socket.addEventListener( Event.CLOSE, onClientClose ); 
            socket.addEventListener( IOErrorEvent.IO_ERROR, onIOError ); 

            //Send a connect message 
            socket.writeUTFBytes("Connected."); 
            socket.flush(); 

            trace( "Sending connect message" ); 
        } 

        public function socketDataHandler(event:ProgressEvent):void 
        { 
            var socket:Socket = event.target as Socket 

            //Read the message from the socket 
            var message:String = socket.readUTFBytes( socket.bytesAvailable ); 
            trace( "Received: " + message); 

            // Echo the received message back to the sender 
            message = "Echo -- " + message; 
            socket.writeUTFBytes( message ); 
            socket.flush(); 
            trace( "Sending: " + message ); 
        } 

        private function onClientClose( event:Event ):void 
        { 
            trace( "Connection to client closed." ); 
            //Should also remove from clientSockets array... 
        } 

        private function onIOError( errorEvent:IOErrorEvent ):void 
        { 
            trace( "IOError: " + errorEvent.text ); 
        } 

        private function onClose( event:Event ):void 
        { 
            trace( "Server socket closed by OS." ); 
        } 
}}

So what do I have to do here? I believe the package is "importing", but when I use addEventListener I get that error thrown at me. Again, this only happens when I publish with AIR. What do I do?

Upvotes: 0

Views: 97

Answers (3)

spring
spring

Reputation: 18487

I am surprised that works in flash. Maybe you are not describing things fully.

In the code below you are trying to add an eventlistener with a Class as the function to be called when the event occurs.

import TCP;
addEventListener(Event.ENTER_FRAME, TCP);

So the error is correct:

1067: Implicit coercion of a value of type Class to an unrelated type Function.

It is not clear from your question what you are trying to do with the event listener.

Upvotes: 0

ozmachine
ozmachine

Reputation: 201

There should be no reason to add an enterframe listener. It looks like the TCP class should function is is...

var tcp:TCP = new TCP();

Should instantiate the class. Its constructor will then try to make a socket connection.

Upvotes: 1

Nemi
Nemi

Reputation: 1032

Error is correct, you should add event listener like:

addEventListener(Event.ENTER_FRAME, onEnterFrameFunction);

Then in onEnterFrameFunction, you can use the TCP class:

var tcp:TCP = new TCP();

Upvotes: 0

Related Questions