簡體   English   中英

在flex中放置actionscript的位置?

[英]where to put actionscript in 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>

首先,確保使用帶有大寫S的fx:Script ; 不是小寫的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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM