| By Nima Azimi | Article Rating: |
|
| November 1, 2005 09:00 AM EST | Reads: |
33,761 |
Step #3:
Now, we must connect to server. This part is very similar in Flash and Director. We must pass a RTMP url and an instance name to the Connect method. In this example room01 is an instance name for our connection. We use instance names in applications that hold several simultaneous connections (a multi-room chat for example).
In Flash:
client_nc.Connect("rtmp://localhost/
myProjectDirectory/room01");
In Director:
pNetConn.Connect("rtmp://localhost/
myProjectDirectory/room01")
Step #4:
Now it's getting fun! To create the remote shared object for our text sharing we use the "getRemote" method of SharedObject. The first parameter of the getRemote method is an object name. client_nc.uri is the URI of the NetConnection. The shared object will use it to connect to the server. The third parameter is a Boolean that indicates whether our shared object is persistent on the server or not. If we set it to true our remote shared object saves its value, thus other users that connect later receive the last value of the shared object from the server. Its value remains even when all users disconnect from server. In this sample we don't want our text to be persistent to the server. Next to this line of code we connect our created shared object to NetConnection that was constructed earlier.
In Flash:
text_so = SharedObject.getRemote("sharedText", client_nc.
uri , false);
text_so.connect(client_nc);
To create a shared object in director we must first get the SharedObject binding from Flash into a variable and then use it. The getVariable function helps us to do so. We use false for second parameter as "returnValueOrReference", because we want to get an object of Flash not its value.
In Director:
mySharedObject = pSprite.getVariable
("SharedObject", false)
text_so = mySharedObject.getRemote("sharedText", pNetConn.
uri. false);
text_so.connect(pNetConn)
Step #5:
When something changes in the shared object, the server sends a synchronization message and an onSync handler change the information on client movie. This way when a user changes a property assigned to a remote shared object, with the help of this onSync handler, all connected users could see those changes.
The "list" parameter is an array that contains information about changes in our textbox. When a user changes a remote shared object by typing a character in textbox with instance name "TypingStage", the server sends a message and onSync handler trigger on all client's machines. "list" array has two properties, name and code. If any change occurs, the code property is set to "change" and when the changes assign to our textbox this property is set to "success". Any change adds an entry in the list array. Thus, when onSync handler triggers, we must test the "list" array to find out any entry with "change" value and if so, update our textbox.
This handler is implemented with "function literal" method again, in Flash.
In Flash:
text_so.onSync = function(list){
for (var i=0; i<list.length; i++){
if (list[1].name == "textValue"
&& list[i].code != "success") {
TypingStage.text = text_so.data.
textValue;
Break;
}
}
};
Again, we use setCallBack to implement this code.
In Director:
pSprite.setCallBack(text_so,
"onSync", #mySyncTexts, me)
on mySyncTexts (me, this, aList)
repeat with i=0 to (i<aList.length)
if (aList[i].name = "textValue" AND
aList[i].code <> "success") then
member("dirTypingStage").text =
text_so.data.textValue
exit repeat
end if
end repeat
end
Step #6:
What happens when you type a character in the "dirTypingStage" textbox? You change a textbox property and this change must be seen by other users, therefore, remote shared objects should update. This is very straightforward in Flash by using the "onChanged" event of the Flash textbox object, but in Director the story is a bit different.
In Flash:
TypingStage.onChanged = function(){
text_so.data.textValue = TypingStage.
text;
};
In Director, we have two methods to implement this equivalent of the block of code above. The first method that I prefer for this sample is to use "the keyUpScript" in the "on StartMovie" handler. We tell Director that whenever a keyUp event has occurred, trigger the "textChanged" handler that is assigned to "the keyUpScript". Remember that the "keyUpScript" and "textChanged" handler must defined in a movie script. The second useful method when we have several textboxes or fields, is to assign an "on keyUp" handler to the textbox itself as a behavior script. In this way when a user types in textboxes other than the "dirTypingStage" textbox, there is not any assignment to remote shared. But in the former method any typing from user in any textbox in the stage causes the call of "onSync" handler from the server.
In Director:
on StartMovie
the keyUpScript = "textChanged"
end
on textChanged
text_so.data.textValue =
member("dirTypingStage").text
end
The complete listings of this sample are shown in Listings 1 & 2. After publishing your completed project you can test it and see the result. Run your ".htm" file twice and while typing in the textbox at one of them, see the other textbox. If you built a custom textbox that does something like a text editor for a foreign language character sets, there is no need to build same custom textbox from the beginning in Flash to work with FlashCom. The solution is to use FlashCom inside Director!
Sample Two - (Shared Ball)
In this sample we want to create a shared ball (2D circle sprite) so that any users visiting our Shockwave page can change the position of the ball with their mouse, while other users see that ball moving on the stage. This is a technique that is used at a higher level in several multi-player games.
Steps 1 and 2 are the same as previous sample, step 3 and 4 are very similar, but with small exceptions. In step 3 we use a new path as RTMP url and in step 4 we use "ball_so" as the name of our shared object and use "position" as the first parameter of getRemote method.
Step #5:
We must now write an onSync handler. When a user changes the position of the ball, this changes what is stored in the x and y properties of our sharedObject. Data must be assigned to the ball sprite location properties (or ball movieClip in Flash), on the other client's movie. When this is done, all users see the repositioning of the ball at the same time.
In Flash:
ball_so.onSync = function(list) {
sharedBall_mc._x = ball_so.data.x;
sahredBall_mc._y = ball_so.data.y;
end
In Director:
pSprite.serCallBack(ball_so,
"onSync", #mySyncBall, me)
on mySyncBall me, this, aList
sprite("Ball").LocH = ball_so.data.x
sprite("Ball").LocV = ball_so.data.y
end
Step #6:
When a user changes the ball position with their mouse, this change must be assigned to x and y properties of "ball_so.data". When those properties are changed, the server sends a synchronization message and our "onSync" handler triggers the other users' machines. This code in Flash is implemented in the "onPress" handler of the ball movieClip but for the best performance in Director, we implement it on "exitframe" handler of a frame behavior, using traditional Director-style event handling.
In addition to setting the x and y properties of shared object, we must ensure that the user does not move ball out of the stage boundaries. Therefore, if our stage size is 640 by 480, we test the position of the ball and if it is 50 pixels (depending on the size of the ball) out of the stage boundaries, we move it back into the stage.
In Flash:
sharedBall_mc.onPress =
function(){
this.onMouseMove =
function(){
ball_so.data.x = this._x =
_root._xmouse;
ball_so.data.y = this._y =
_root._ymouse;
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;
}
};
sharedBall_mc.onRelease = sharedBall_
mc.onReleaseOutSide = function()
delete this.onMouseMove;
};
Published November 1, 2005 Reads 33,761
Copyright © 2005 SYS-CON Media, Inc. — All Rights Reserved.
Syndicated stories and blog feeds, all rights reserved by the author.
More Stories By Nima Azimi
Nima Azimi is a software engineer, multimedia project manager,
consultant and programmer on variety projects. His projects include
educational "How does it works" titles for children education with
real-time 3D content. He has worked with Director for over four years,
and he currently teaches courses in Director programming and multimedia.
In his spare time he makes highly detailed photorealistic 3D scenes as a
3D artist and writes video game scripts and gameplay ideas that he
wishes to develop into full games at within the near future.
![]() |
Rob Sandie 11/01/05 03:03:15 PM EST | |||
Just a note: with the new release this month it will not be FlashComm or Flash Communication Server. The beta has it anounced as Flash Media Server 2. |
||||
![]() |
gamer4all 11/01/05 10:00:35 AM EST | |||
|| Using authentication, you could give different users different roles in the system, assigning some users more power in the system than others || I've tried this. It works real well. |
||||
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Adobe Unveils LiveCycle Enterprise Suite 2 for Deployment in the Cloud
- Adobe Flex Developer Earns $100K in New York City
- Adobe May Cooperate with Apple to Transplant Flash Player to iPhone
- Ph.D. in Twitter Anyone?
- Eolas Sues the Internet
- Adobe LiveCycle Enterprise Suite 2 for Cloud Computing
- Adobe Betas Target RIAs and Cloud Computing
- Special Report on the Emerging Cloud Computing Trend
- Adobe Cans Another 9% of its Workforce
- My Thoughts on Ulitzer
- Ulitzer.com Named Exclusive "New Media" Sponsor of Cloud Computing Conference & Expo
- Ulitzer Live! New Media Conference & Expo
- Adobe’s Aiming ColdFusion at Multiple Clouds
- Eval JavaScript in a Global Context
- Fig Leaf Software to Exhibit at Government IT Conference & Expo
- Cloud Executives Feature on Cloud Computing Expo Power Panel
- Software Flexibility in the Cloud - Part 4 of 5
- Cloud Computing Journal: Adobe to Deliver ColdFusion in the Cloud
- Is Microsoft as Free as Open Source?
- Adobe Reader Sued
- Adobe Unveils LiveCycle Enterprise Suite 2 for Deployment in the Cloud
- Where Are RIA Technologies Headed in 2008?
- Cover Story: How to Increase the Frame Rates of Your Flash Movies
- AJAX World RIA Conference & Expo Kicks Off in New York City
- Your First Adobe Flex Application with a ColdFusion Backend
- Adobe Flex 2: Advanced DataGrid
- i-Technology Blog: Death-Knell For "Rich Media? Hardly!
- Adobe/Macromedia - Microsoft, Look Out!
- How To Create a Photo Slide Show ...
- Adobe Flex Interface Customization - Themes, Styles, Skins
- Personal Branding Checklist
- Has the Technology Bounceback Begun?
- "Real-World Flex" by Adobe's Christophe Coenraets




































