Wednesday, January 28th, 2009...11:22 pm
Problem with movie in class
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:
- Create the Object
- Add and Event Listener
- Connect the Object
- Write the Event Handler
Here is how that works for each of them
NetConnection Object
- CREATE
client_nc = new NetConnection(); - ADD EVENT LISTENER
client_nc.addEventListener(NetStatusEvent.NET_STATUS, ncStatus); - CONNECT
client_nc.connect(“rtmp://mmp.bmcc.cuny.edu/example”); - 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
- CREATE
my_so = SharedObject.getRemote(“ChatObject”, client_nc.uri); - ADD EVENT LISTENER
my_so.addEventListener(SyncEvent.SYNC, soSync); - CONNECT
my_so.connect(client_nc); - 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
Leave a Reply
You must be logged in to post a comment.