Question

I'm trying to use a sharedObject to coordinate the movement of several objects on the stage between multiple game players. I think that I'm very close but I seem to be having a problem converting a string stored in the sharedObject into an instance of the Ball class.

My problem is in the soSync function. I think I need to convert into an object the value of 'objectMoved' that is stored in the sharedObject. I can trace the name of the selected object, but I cannot retrieve it as an object. How do I store the selected object in a sharedObject and then use it to coordinate motion with the other game players?

<?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>
Était-ce utile?

La solution

I think you could use getChildByName("String/ObjectName here").

So in your case...

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

Alternatively, could you not just add the physical object itself when using setProperty as opposed to the objects name. Like so...

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

Autres conseils

Your Ball object seems to be a DisplayObject, which are unable to be saved in a shared object as a whole. You can, however, instantiate a Ball object, read properties out of the SO and assign them to your ball. The properties in question seem to be x and y, and not objectMoved, as your ball's name will most likely be in line of instance389 which does not spell a thing if you lose the naming context, and even if not, you can only find a child by name if it still exists. So, if you have only one object of type Ball, you assign it the X and Y read from the SO, if not, you should store several balls in your SO, so you will instantiate them later and preserve all of their coordinates.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top