2

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

球.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>
4

2 回答 2

7

我认为您可以使用 getChildByName("String/ObjectName here")。

所以在你的情况下......

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

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

remoteSO.setProperty("objectMoved",e.currentTarget);
于 2012-12-18T16:44:59.620 回答
0

您的 Ball 对象似乎是一个 DisplayObject,无法将其作为一个整体保存在共享对象中。但是,您可以实例化一个 Ball 对象,从 SO 中读取属性并将它们分配给您的球。有问题的属性似乎是xand y,而不是objectMoved,因为如果您失去命名上下文,您的球的名字很可能会符合instance389which 不拼写的东西,即使没有,您也只能通过名字找到一个孩子,如果它仍然存在。因此,如果您只有一个 Ball 类型的对象,则将其指定为从 SO 读取的 X 和 Y,如果没有,您应该在 SO 中存储几个球,以便稍后实例化它们并保留它们的所有坐标。

于 2012-12-18T18:00:42.487 回答