我发布了一个我在AIR 2.6而不是Flash Player中制作的Flash游戏,因为这是我使用TCP工作的唯一方法。出于某种原因,这样做不允许我导入包,似乎......
如果我通过Flash Player发布,则此代码可以正常工作:
import TCP;
addEventListener(Event.ENTER_FRAME, TCP);
当我执行相同的操作但使用AIR 2.6发布时,我收到此错误:
1067:将类型Class的值隐式强制转换为不相关的类型函数。
这是整个包裹:
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." );
}
}}
那我该怎么做呢?我相信包是#34;导入",但是当我使用addEventListener时,我得到了错误。同样,这只发生在我使用AIR发布时。我该怎么办?
答案 0 :(得分:1)
没有理由添加enterframe侦听器。看起来TCP类的功能是......
var tcp:TCP = new TCP();
应该实例化该类。然后它的构造函数将尝试建立套接字连接。
答案 1 :(得分:0)
我很惊讶在flash中工作。也许你没有完全描述事情。
在下面的代码中,您尝试添加一个带有Class的eventlistener作为事件发生时要调用的函数。
import TCP;
addEventListener(Event.ENTER_FRAME, TCP);
所以错误是正确的:
1067:将类型Class的值隐式强制转换为不相关的类型函数。
从您的问题中不清楚您要对事件监听器做什么。
答案 2 :(得分:0)
错误是正确的,你应该添加如下的事件监听器:
addEventListener(Event.ENTER_FRAME, onEnterFrameFunction);
然后在onEnterFrameFunction中,您可以使用TCP类:
var tcp:TCP = new TCP();