简体   繁体   中英

where to put actionscript in flex?

I am not understanding something I believe that flex can run actionscript but every time I try I get different errors.

this time my error is:

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

I keep looking at flex tutorials and it's almost like they assume that one knows the ide hence they are just showing the .as code.

I think that everything flash can do flex can do. So why be restricted to only one.

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

First, make sure you use fx:Script with a capital S; not a lowercase S.

Then move your fx:Script tag out of the fx:Declaration and your code will compile:

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

The fx:Declaration tag is primarily used for MXML components that are non-visual, such as validators or services. While fx:Script is clearly non-visual it is not usually embeded inside fx:Declaration .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM