Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 如何将字符串转换为对象_Actionscript 3_Flash_Apache Flex_Flash Media Server - Fatal编程技术网

Actionscript 3 如何将字符串转换为对象

Actionscript 3 如何将字符串转换为对象,actionscript-3,flash,apache-flex,flash-media-server,Actionscript 3,Flash,Apache Flex,Flash Media Server,我尝试使用sharedObject来协调舞台上多个游戏玩家之间的多个对象的移动。我想我已经非常接近了,但是我似乎在将存储在sharedObject中的字符串转换为Ball类的实例时遇到了问题 我的问题是soSync函数。 我想我需要将存储在sharedObject中的'objectMoved'的值转换成一个对象。我可以跟踪选定对象的名称,但无法将其作为对象检索。 如何将选定对象存储在sharedObject中,然后使用它与其他游戏玩家协调运动 <?xml version="1.0" enc

我尝试使用sharedObject来协调舞台上多个游戏玩家之间的多个对象的移动。我想我已经非常接近了,但是我似乎在将存储在sharedObject中的字符串转换为Ball类的实例时遇到了问题

我的问题是soSync函数。 我想我需要将存储在sharedObject中的'objectMoved'的值转换成一个对象。我可以跟踪选定对象的名称,但无法将其作为对象检索。 如何将选定对象存储在sharedObject中,然后使用它与其他游戏玩家协调运动

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="init()">
    <mx:Script>
        <![CDATA[
            import mx.core.UIComponent;
            public var nc:NetConnection;
            private var connectionURL:String = "rtmp://server address...";
            public var remoteSO:SharedObject; 
            private var checker:Ball;

            public function init():void{
                //Create new checkers; place them and create listeners
                for (var i:int = 0; i < 3; i++){
                    checker = new Ball;
                    checker.x = 150 + (i * 110);
                    checker.y = 150;    
                    checker.name = String(i);
                    this.addChild(checker);
                    checker.addEventListener(MouseEvent.MOUSE_DOWN,handleMouseDown);
                }
                // Establish connection to the server
                nc = new NetConnection();           
                //Listener triggered by the NetConnection connection 
                nc.addEventListener(NetStatusEvent.NET_STATUS, netStatusHandler);
                nc.connect(connectionURL);
            }
            private function netStatusHandler(e:NetStatusEvent):void {
                if(e.info.code == "NetConnection.Connect.Success"){
                    createRemoteSO();
                }
            }
            private function handleMouseDown(e:MouseEvent):void{
                //Show which object has been selected
                objectInfo.text = "Clicked: "+String(e.currentTarget);
                e.currentTarget.startDrag();
                e.currentTarget.addEventListener(MouseEvent.MOUSE_MOVE,dragging);
                e.currentTarget.addEventListener(MouseEvent.MOUSE_UP,handleMouseUp)
                //Update the remote shared object from this client     
                function dragging(e:MouseEvent):void{  
                    objectInfo.text = "Dragging: "+String(e.currentTarget);
                    //Set the shared object         
                    remoteSO.setProperty("objectMoved",e.currentTarget.name);               
                    remoteSO.setProperty("x",e.currentTarget.x);                
                    remoteSO.setProperty("y",e.currentTarget.y);
                }
                function handleMouseUp(e:MouseEvent):void{
                    e.currentTarget.stopDrag();
                    objectInfo.text = ""
                    e.currentTarget.removeEventListener(MouseEvent.MOUSE_MOVE,dragging)
                }
            }
            private function createRemoteSO():void{
                // Create reference to the remote shared object
                remoteSO = SharedObject.getRemote("remoteSO",nc.uri,false);
                remoteSO.connect(nc);
                //Listener to handle changes to the remote shared object 
                remoteSO.addEventListener(SyncEvent.SYNC, soSync);                      
            }   
            //Called when a change is made to the remote shared object by other clients
            private var counter:int;
            private function soSync(se:SyncEvent):void { 
                if(remoteSO.data.objectMoved){
                    this.getChildByName(remoteSO.data.objectMoved).x = remoteSO.data.x;
                    this.getChildByName(remoteSO.data.objectMoved).y = remoteSO.data.y;
                }else{
                    trace("Object does not exist")
                }
            } 
        ]]>
    </mx:Script>        
    <mx:Text x="147" y="51" text="Selected object"/>
    <mx:TextArea  id="objectInfo" x="237" y="50"/>
</mx:Application>

Ball.mxml

<?xml version="1.0" encoding="utf-8"?>
<mx:UIComponent xmlns:mx="http://www.adobe.com/2006/mxml"  creationComplete="init()">
<mx:Script>
    <![CDATA[
    import mx.events.FlexEvent;

    private function init():void{
        var child:Shape = new Shape();
        child.graphics.beginFill(0xff0000, .5);
        child.graphics.drawCircle(0, 0, 50);
        child.graphics.endFill();
        this.addChild(child);
    }
    ]]>
</mx:Script>    
</mx:UIComponent>

我认为您可以使用getChildByName(“这里的String/ObjectName”)

所以在你的情况下

this.getChildByName(remoteSO.data.objectMoved).y = remoteSO.data.y;
或者,在使用setProperty而不是对象名称时,您是否可以只添加物理对象本身。就像这样

remoteSO.setProperty("objectMoved",e.currentTarget);

您的Ball对象似乎是一个DisplayObject,无法作为一个整体保存在共享对象中。不过,您可以实例化一个Ball对象,从SO中读取属性并将它们指定给您的球。所讨论的属性似乎是
x
y
,而不是
objectMoved
,因为球的名称很可能在
实例389
中,如果丢失了命名上下文,则不会拼写任何东西,即使没有,也只能在仍然存在的情况下按名称找到子对象。因此,如果您只有一个Ball类型的对象,您可以将从So读取的X和Y分配给它,如果没有,您应该在So中存储几个球,以便稍后实例化它们并保留它们的所有坐标。

非常感谢!第一个答案是正确的,但我无法让第二个答案起作用。我已经更正了原始代码,做了一些小的修改,以确保代码是正确的。这是伟大的,也帮助了我!感谢您修改了上面的+1代码以显示正确的解决方案