Friday, January 7, 2011

SmartFoxServer Tutorial: Private Messaging

Hello again.

In this tutorial, as you can probably tell from the title of this post, is about private messaging. Anyway, let's get started with this tutorial. By the way, for this tutorial I'm using the SmartFoxServer avatarChat example file.

First of all, you have to make a button which will open the send message window.

Now, I just labeled mine with the text "Open." You can label yours with something else, such as send if you want. You can also give the button an instance name if you want. This isn't required, but I do that with all my symbols.

The next thing we will do, is make the private message window. In the window, this is where you will see the messages received and it will also be used to send messages. Here is the window I made:

I have multiple items in my private message window, so now I'm going to explain each one to you. Also, my window's instance name is pmWindow.

1 - Dynamic text box, which I gave an instance name of whoToSend.
2 - Input text box, which I gave an instance name of msgToSend.
3 - Button symbol, which will be used to send the private message to the desired user.
4 - Dynamic text box, which I gave an instance name of msgText.
5 - This is optional, but I made the top part of my window draggable.
6 - Button symbol, which will be used to close the window.

Now all that we need to do is code the updates to the avatarChat file. For the main coding, I'm going to just use functions for simplicity.

I'm just going to paste on the coding that I added to the chat frame of the Flash file at this point.

_root.pmWindow._visible = false;

function openPm() {
var uPm = _root.userList_lb.getSelectedItem().data;
var targetUser = _root.userList_lb.getSelectedItem().label;
if (uPm != _root.smartfox.myUserId()) {
_global.tUser = targetUser;
_global.uPm = uPm;
_root.pmWindow._visible = true;
_root.pmWindow.whoToSend.text = "Do you want to send a private message to " + _global.tUser + "?";
} else {
trace("You can't send a private message to yourself.");

function sendPm() {
var pMsg = _root.pmWindow.msgToSend.text;
if (pMsg != "") {
_root.smartfox.sendPrivateMessage(pMsg, _global.uPm);
_root.pmWindow.msgToSend.text = "";

function closePm() {
_root.pmWindow._visible = false;

smartfox.onPrivateMessage = function(msg:String, sender:User) {
_root.pmWindow._visible = true;
_root.pmWindow.msgText.text += newline + sender.getName() + ": " + msg;
I wrote that coding all that coding at the very end of the chat frame coding. That's all the frame coding, so we're almost done. All we have left to do is add the button coding. On the send private message button, add the following coding.

on (release) {
Now, on the close window button, add this coding:

on (release) {
Also, on the open window button, be sure to add this coding:

on (release) {
Now, to use the private message feature, you have to click a username on the userlist and click the open button. You can't click on yourself because this was just a quick little tutorial I rushed through.

Anyway, that's all for this tutorial.

If you have any questions or comments, then be sure to comment on this post and I'll reply as soon as I get a chance!

Source File: Download


  1. That must have took hours to wrote! I appreciate you posting these tutorials. I love reading them ;)

  2. Hey CB,

    I am trying to make a buddy list.

    I made the buddy list window and dragged a userlist onto it.

    However when I test the game no users appear in the userlist?

    Do you know what I have done wrong?


  3. MrRd78: The coding used by default in the avatarChat example file doesn't allow you to do that becaus ethe normal path of the userList_lb instance is located on the root, not somewhere else. When you change the location of an instance, you have to change it in the coding as well.

  4. Hmm... When i send the message to someone, it doesn't come up on the PM window...?

  5. Thanks CB after a long thought I worked this out :) and also the player card tutorial is great.

  6. Tree Line 1: Download the source file as provided in the post and compare your coding with it, otherwise try double checking your coding. My example file worked fine for me, but you have to try it on two different SWF files.

  7. Still doesn't work... I even tried copy and pasting everything from that .fla

  8. Tree Line 1: What version of Flash are you using and what version of SmartFoxServer are you testing this with?