Wednesday, January 28th, 2009...11:22 pm

Problem with movie in class

Jump to Comments

This post is about why my code wasn’t working and also explains more about the parts that are needed to make a connection work in general. While I’m sorry I didn’t catch the mistakes in class at least you got to see that we all make mistakes and forget things.

My code wasn’t working at the end of class because I forgot two very important things with the shared object. I didn’t create it and I didn’t connect it. It’s a good time to point out that both the SharedObject and the NetConnection object have four things they need to work properly:

  1. Create the Object
  2. Add and Event Listener
  3. Connect the Object
  4. Write the Event Handler

Here is how that works for each of them

NetConnection Object

  1. CREATE
    client_nc = new NetConnection();
  2. ADD EVENT LISTENER
    client_nc.addEventListener(NetStatusEvent.NET_STATUS, ncStatus);
  3. CONNECT
    client_nc.connect(“rtmp://mmp.bmcc.cuny.edu/example”);
  4. EVENT HANDLER
    function ncStatus(evt:NetStatusEvent) {
         trace(evt.info.code);
    }

Remember the first three lines go in the Main constructor function and the ncStatus function separate.

SharedObject

  1. CREATE
    my_so = SharedObject.getRemote(“ChatObject”, client_nc.uri);
  2. ADD EVENT LISTENER
    my_so.addEventListener(SyncEvent.SYNC, soSync);
  3. CONNECT
    my_so.connect(client_nc);
  4. EVENT HANDLER
    function soSync(evt:SyncEvent) {
        trace(“synced”);
    }

Again the first three steps are in the constructor function and the event handler is separate.

How you create the SharedObject is a little different from how you create most objects. Instead of using the new keyword ( like new SharedObject() ), you use the getRemote() method. This method needs two parameters. The first is the name of the SharedObject, I called it “ChatObject” here. This name is a string that you make up. It is important because if two movies use a different name for the SharedObject then they will not talk to each other. The second parameter is is the same connection string you usedin step 3 of the NetConnection object. However instead of having to write it twice you can just use client_nc.uri and it will use the URI from the NetConnection Object.

Also notice that when you connect the SharedObject you add a reference to the client_nc again (without the uri part this time).

Here is a file with an example of those connections

main1

Share


Leave a Reply

You must be logged in to post a comment.