在flex中放置actionscript的位置?

时间:2013-12-20 16:37:49

标签: actionscript-3 flex

我不理解我认为flex可以运行actionscript但我每次尝试时都会遇到不同的错误。

这次我的错误是:

Description Resource    Path    Location    Type
Could not resolve <fx:script> to a component implementation.    as.mxml /ar/src line 7  Flex Problem

我一直在看flex教程,这几乎就像他们认为人们知道了ide因此他们只是显示.as代码。

我认为一切flash都可以做flex。那么为什么只限于一个。

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
           xmlns:s="library://ns.adobe.com/flex/spark" 
           xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">
<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
    <fx:script>
        var socket:XMLSocket;

        stage.addEventListener(MouseEvent.CLICK, doConnect);


        function doConnect(evt:Event):void{
            stage.removeEventListener(MouseEvent.CLICK, doConnect);
            socket = new XMLSocket("127.0.0.1", 9001);
            socket.addEventListener(Event.CONNECT, onConnect);
            socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
            }


        function onConnect(evt:Event):void{
            trace("Connected");
            socket.removeEventListener(Event.CONNECT, onConnect);
            socket.removeEventListener(IOErrorEvent.IO_ERROR, onError);
            socket.addEventListener(DataEvent.DATA, onDataReceived);
            socket.addEventListener(Event.CLOSE, onSocketClose);                
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
            }


        function onSocketClose(evt:Event):void{
            trace("Connection Closed");
            stage.removeEventListener(KeyboardEvent.KEY_UP, keyUp);
            socket.removeEventListener(Event.CLOSE, onSocketClose);
            socket.removeEventListener(DataEvent.DATA, onDataReceived);
            }

        function keyUp(evt:KeyboardEvent):void{
            if(evt.keyCode == 81){
                socket.send("exit");
                }
            else{
                socket.send(evt.keyCode);
                }}

        function onDataReceived(evt:DataEvent):void{
            try{
                trace( "From Server:", evt.data );
                }
            catch(e:Error){
                trace('error');
                }}



        function onError(evt:IOErrorEvent):void{
            trace("Connect failed");
            socket.removeEventListener(Event.CONNECT, onConnect);
            socket.removeEventListener(IOErrorEvent.IO_ERROR, onError);
            stage.addEventListener(MouseEvent.CLICK, doConnect);
            }
    </fx:script>
</fx:Declarations>

1 个答案:

答案 0 :(得分:2)

首先,确保使用fx:Script的大写字母S;不是小写的S.

然后将fx:Script标记移出fx:Declaration,您的代码将会编译:

<fx:Declarations>
    <!-- Place non-visual elements (e.g., services, value objects) here -->
</fx:Declarations>

    <fx:Script>
        var socket:XMLSocket;

        stage.addEventListener(MouseEvent.CLICK, doConnect);


        function doConnect(evt:Event):void{
            stage.removeEventListener(MouseEvent.CLICK, doConnect);
            socket = new XMLSocket("127.0.0.1", 9001);
            socket.addEventListener(Event.CONNECT, onConnect);
            socket.addEventListener(IOErrorEvent.IO_ERROR, onError);
            }


        function onConnect(evt:Event):void{
            trace("Connected");
            socket.removeEventListener(Event.CONNECT, onConnect);
            socket.removeEventListener(IOErrorEvent.IO_ERROR, onError);
            socket.addEventListener(DataEvent.DATA, onDataReceived);
            socket.addEventListener(Event.CLOSE, onSocketClose);                
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUp);
            }


        function onSocketClose(evt:Event):void{
            trace("Connection Closed");
            stage.removeEventListener(KeyboardEvent.KEY_UP, keyUp);
            socket.removeEventListener(Event.CLOSE, onSocketClose);
            socket.removeEventListener(DataEvent.DATA, onDataReceived);
            }

        function keyUp(evt:KeyboardEvent):void{
            if(evt.keyCode == 81){
                socket.send("exit");
                }
            else{
                socket.send(evt.keyCode);
                }}

        function onDataReceived(evt:DataEvent):void{
            try{
                trace( "From Server:", evt.data );
                }
            catch(e:Error){
                trace('error');
                }}



        function onError(evt:IOErrorEvent):void{
            trace("Connect failed");
            socket.removeEventListener(Event.CONNECT, onConnect);
            socket.removeEventListener(IOErrorEvent.IO_ERROR, onError);
            stage.addEventListener(MouseEvent.CLICK, doConnect);
            }
    </fx:Script>

fx:Declaration标记主要用于非可视化的MXML组件,例如验证程序或服务。虽然fx:Script显然是非可视的,但它通常不会嵌入fx:Declaration内。