Monday, February 28, 2011

SmartFoxServer Tutorial: "Mod Magic" Tutorial (Sending Objects)

Hello everyone!

I'm finally back to posting as you can see. I've been really busy with a game I'm working on and other things so I haven't had time to check the blog. Anyway, let's get started on the "Mod Magic" tutorial, or as I like to call "Mod Magic", SmartFoxServer objects.

This technique is very simple to learn, and I'm going to try to explain what each line does carefully enough so that a beginner can understand it, much like the SmartFoxserver Docs already does. Anyway, let's begin now.

First off, I'm using the provided SmartFoxServer avatarChat example file for this tutorial, just like all my other tutorials. So, open your Flash file up and go to the frame you want to add your Mod Magic and open the actions for that frame.

On the SmartFoxServer avatarChat file, I went to the chat frame and added this coding at the very bottom of all the other actions in the movie:

_root.showM1._visible = false;//Hides magic at start
function doMagic(magicName:String) {
var sfsObj = {}; //Makes new object
sfsObj.magicName = magicName;//Adds to object
_root.smartfox.sendObject(sfsObj);//Sends object
if (magicName == "magic1") {//Shows on sender's side
_root.showM1._visible = true;//Shows on sender's side
}//Shows on sender's side
}
smartfox.onObjectReceived = function(o:Object) {
if (o.magicName == "magic1") {//If magicName == magic1
_root.showM1._visible = true;//Show the mod magic to other people, not you
}
}


Now, the first line should tell you that we need to create a symbol in our Flash file. So, what I did was create a very simple rectangle shape and wrote some words on it and made it a movieclip by selecting it and converting it to a symbol by right clicking and choosing the "Convert to Symbol..." option. I gave it the instance name of "showM1", which, for me, stands for "showMagic1."


Now, the next thing to do is to create the doMagic() function, which basically perfoms the magic on the sender's side, then send the magic to all the other clients. So, now we need to create a button or movieclip to call the function. I chose a button. On the button I added this coding:

on (release) {
_root.doMagic("magic1"); //Calls function
}


The next and final thing to do is to setup the smartfox.onObjectReceived handler, which is in the coding above. So now you are able to test your movie. You should be able to see that when a user clicks the button, it shows your "Mod Magic."

NOTE: When the smartfox.sendObject() command is called, since your user is the sender of the object, the object will not be broadcasted to you, so you have to make a code so it shows the mod magic on the sender's side as well, which is exactly what we did.

If you have any questions or comments, please leave them on this post and I'll get back to you as soon as I can!

Source File: Download

4 comments:

  1. Not bad. Very insecure. I recommend migrating to the server side. Much nicer

    ReplyDelete
  2. @Flappi282: It wasn't made to be secure, it was made mainly to introduce the concept to people. xD

    ReplyDelete
  3. What exactly does this do? Shows an object to other users?

    ReplyDelete
  4. @Anonymous: This sends objects to all clients in the room, excluding yourself. It doesn't "show" anythign to anyone unless you code it to do so, which is what I did above.

    ReplyDelete