简体   繁体   English

在flex中放置actionscript的位置?

[英]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. 我不理解我认为flex可以运行actionscript但我每次尝试时都会遇到不同的错误。

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. 我一直在看flex教程,它几乎就像他们认为一个人知道ide因此他们只是显示.as代码。

I think that everything flash can do flex can do. 我认为一切flash都可以做flex。 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; 首先,确保使用带有大写S的fx:Script ; not a lowercase S. 不是小写的S.

Then move your fx:Script tag out of the fx:Declaration and your code will compile: 然后将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>

The fx:Declaration tag is primarily used for MXML components that are non-visual, such as validators or services. fx:Declaration标记主要用于非可视化的MXML组件,例如验证器或服务。 While fx:Script is clearly non-visual it is not usually embeded inside fx:Declaration . 虽然fx:Script显然是非直观的,但通常不会嵌入fx:Declaration

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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