Skip to main content
Known Participant
November 19, 2009
Question

[AS3] Cannot sync sharedObjects

  • November 19, 2009
  • 1 reply
  • 1429 views

Hello, Ive tried converting the SharedObject tutorial [http://www.adobe.com/devnet/flashmediaserver/sample_apps.html] provided by Adobe from AS2 to AS3. When one client moves the ball, the other client should be able to see the ball moving on another swf, and vice versa.

The AS2 one worked fine with my flash media server 3.5 and wampp server on. 

When I tried the AS3 one, the sharedObject cannot sync, even though the connection was successful and the sharedObject "position" was created in the server.   Can anyone help me solve the problem?

stop();

var nc:NetConnection = new NetConnection();

nc.connect("rtmp://localhost/myapplication");

//check whether connection is successful

nc.addEventListener(NetStatusEvent.NET_STATUS, onstatus1);

function onstatus1 (event:NetStatusEvent): void

{

     trace("Level: " + event.info.level + "\nCode: " +  event.info.code);

}

//create and connect the sharedObject

var ball_so = SharedObject.getRemote("position", nc.uri, false);

ball_so.connect(nc);

//onSync function

nc.addEventListener(SyncEvent.SYNC, syncSO);

function syncSO(event:SyncEvent):void

{

     SharedBall_mc.x = ball_so.data.x;

     SharedBall_mc.y = ball_so.data.y;

}

//event listeners for the dragging events

SharedBall_mc.addEventListener(MouseEvent.MOUSE_DOWN, ballStartDrag);

SharedBall_mc.addEventListener(MouseEvent.MOUSE_UP, ballEndDrag);

SharedBall_mc.addEventListener(Event.ENTER_FRAME, ballShadow);

//start drag function

function ballStartDrag(event:MouseEvent):void

{

     SharedBall_mc.startDrag();

     //setting the constraints

     if (SharedBall_mc.x>=stage.width) {

          SharedBall_mc.x = stage.width - 50;

     }

     if (SharedBall_mc.x <=0) {

          SharedBall_mc.x = 50;

     }

     if (SharedBall_mc.y>=stage.height) {

          SharedBall_mc.y = stage.height - 50;

     }

     if (SharedBall_mc.y<=0) {

          SharedBall_mc.y = 50;

     }

}

//when the dragging ends

function ballEndDrag(event:MouseEvent):void{

     SharedBall_mc.stopDrag();

}

function ballShadow (event:Event):void{

    

     //store the x and y position of the ball into the sharedObject

     ball_so.data.x = SharedBall_mc.x;

     ball_so.data.y = SharedBall_mc.y;

   

     //make the shadow follow the ball

     Shadow_mc.x = SharedBall_mc.x+15+(-1*SharedBall_mc.y+400)/4;

     Shadow_mc.y = SharedBall_mc.y/2+220;

     Shadow_mc.alpha = (SharedBall_mc.y+400)/16;

}

    This topic has been closed for replies.

    1 reply

    ghost31379
    Inspiring
    November 19, 2009

    my brain is hurtin and i'm sleepy but.....ok.......you have to have a connection first before you can call  :

    var ball_so = SharedObject.getRemote("position", nc.uri, false);

    so what i usually do is code it like this......

    function onstatus1 (event:NetStatusEvent): void

    {

         trace("connected is: " + nc.connected );
                trace("event.info.level: " + event.info.level);
                trace("event.info.code: " + event.info.code);
               
                switch (event.info.code)
                {
                    case "NetStream.Buffer.Full":
                        break;
                    case "NetStream.Buffer.Empty":
                        break;
                    case "NetConnection.Connect.Success":
                        trace("Congratulations! you're connected");


                        nc.client = this;
                        ball_so = SharedObject.getRemote("position", nc.uri, false);

                        ball_so.connect(nc);

                        //onSync function

                        nc.addEventListener(SyncEvent.SYNC, syncSO);


                       
                        break;
                    case "NetConnection.Connect.Failed":
                    case "NetConnection.Connect.Rejected":
                        trace ("Oops! the connection was rejected");
                        break;
                    case "NetStream.Play.Stop":
                        break;
                    case "NetStream.Play.StreamNotFound":
                        break;
                    case "NetStream.Publish.BadName":
                        break;
                }

    }

    everything else can probably stay the same but i havent tested your code as of yet cus im bout to crash and get some rest......but i have connected many shared objects and sync using fms and as3

    Known Participant
    November 20, 2009

    Thanks ghost31379. I've added in your codes but it still doesn't seem to be able to sync even though the connection was successful and I was able to trace out ball_so.data.x. What could be the problem

    The output was:

    connected is: true
    event.info.level: status
    event.info.code: NetConnection.Connect.Success
    Congratulations! you're connected
    275.40000000000003
    275.40000000000003

    .

    .

    .

    ======================================================================

    The codes are now like this:

    stop();

    var nc:NetConnection = new NetConnection();
    nc.connect("rtmp://localhost/myapplication2/");

    //check whether connection is successful
    nc.addEventListener(NetStatusEvent.NET_STATUS, onstatus1);

    function onstatus1 (event:NetStatusEvent): void

    {

         trace("connected is: " + nc.connected );
                trace("event.info.level: " + event.info.level);
                trace("event.info.code: " + event.info.code);
              
                switch (event.info.code)
                {
                    case "NetStream.Buffer.Full":
                        break;
                    case "NetStream.Buffer.Empty":
                        break;
                    case "NetConnection.Connect.Success":
                        trace("Congratulations! you're connected");


                        nc.client = this;
                        ball_so = SharedObject.getRemote("position", nc.uri, false);

                        ball_so.connect(nc);

                        //onSync function

                        nc.addEventListener(SyncEvent.SYNC, syncSO);


                        break;
                    case "NetConnection.Connect.Failed":
                        break;
                    case "NetConnection.Connect.Rejected":
                        trace ("Oops! the connection was rejected");
                        break;
                    case "NetStream.Play.Stop":
                        break;
                    case "NetStream.Play.StreamNotFound":
                        break;
                    case "NetStream.Publish.BadName":
                        break;
                }

    }

    function syncSO(event:SyncEvent):void
    {
        SharedBall_mc.x = ball_so.data.x;
        SharedBall_mc.y = ball_so.data.y;
    }

    //event listeners for the dragging events
    SharedBall_mc.addEventListener(MouseEvent.MOUSE_DOWN, ballStartDrag);
    SharedBall_mc.addEventListener(MouseEvent.MOUSE_UP, ballEndDrag);
    SharedBall_mc.addEventListener(Event.ENTER_FRAME, ballShadow);


    //start drag function
    function ballStartDrag(event:MouseEvent):void
    {
            SharedBall_mc.startDrag();
           
            //setting the constraints
            if (SharedBall_mc.x>=stage.width) {
            SharedBall_mc.x = stage.width - 50;
            }
            if (SharedBall_mc.x <=0) {
                SharedBall_mc.x = 50;
            }
            if (SharedBall_mc.y>=stage.height) {
                SharedBall_mc.y = stage.height - 50;
            }
            if (SharedBall_mc.y<=0) {
                SharedBall_mc.y = 50;
            }

    }
           
    //when the dragging ends       
    function ballEndDrag(event:MouseEvent):void{
        SharedBall_mc.stopDrag();
    }


    function ballShadow (event:Event):void{
       
        //store the x and y position of the ball into the sharedObject
        ball_so.data.x = SharedBall_mc.x;
        ball_so.data.y = SharedBall_mc.y;
       
        //make the shadow follow the ball
        Shadow_mc.x = SharedBall_mc.x+15+(-1*SharedBall_mc.y+400)/4;
        Shadow_mc.y = SharedBall_mc.y/2+220;
        Shadow_mc.alpha = (SharedBall_mc.y+400)/16;

         trace(ball_so.data.x);
    }