如何将字符串转换为对象

时间:2012-12-18 15:25:54

标签: actionscript-3 flash flex flash-media-server

我正在尝试使用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>

2 个答案:

答案 0 :(得分:7)

我认为你可以使用getChildByName(“String / ObjectName here”)。

所以在你的情况下...

this.getChildByName(remoteSO.data.objectMoved).y = remoteSO.data.y;

或者,您是否可以在使用setProperty而不是对象名称时添加物理对象本身。像这样......

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

答案 1 :(得分:0)

您的Ball对象似乎是一个DisplayObject,它无法作为一个整体保存在共享对象中。但是,您可以实例化Ball对象,从SO中读取属性并将它们分配给您的球。有问题的属性似乎是xy,而不是objectMoved,因为您的球的名称很可能与instance389一致,如果不是拼写的话你丢失了命名上下文,即使没有,你也只能找到一个名字,如果它仍然存在。所以,如果你只有一个Ball类型的对象,你可以为它指定从SO读取的X和Y,如果没有,你应该在你的SO中存储几个球,所以你稍后会实例化它们并保留它们的所有坐标。 / p>