lang="en-US"> SmartFoxServer Flash AS3 Tutorial: Using Classes –  Design1online.com, LLC

SmartFoxServer Flash AS3 Tutorial: Using Classes

Okay so one of the thing I’ve learned about SmartFoxServer is that the documentation is hard to search through and the sample code they have available really doesn’t cover a lot of standard coding practices. In trying to use SmartFoxServer with classes I ran into several problems. After several hours of fussing with it I’ve finally created a working piece of code. If anyone else has a better way of doing this I’d LOVE to see it. Or you can be lazy and download my working code. You might also be interested in learning how to create a custom login extension using MySQL.

This goes in the main .fla file:

import it.gotoandplay.smartfoxserver.*;
import it.gotoandplay.smartfoxserver.data.*;
import com.design1online.gameinstance.*;

//class variables
var smartfox:SmartFoxClient  = new SmartFoxClient();
var game:gameinstance = new gameinstance();

//smartfox connection variables UPDATE THESE
var zone:String = ""; //zone in smartfox server
var ip = ""; //IP address to a server running smartfox
var port = 9339; //the port smartfox is listening to
var room = "Lobby" //the room to join in that zone

var username:String = "player" + Math.round(1000 * Math.random());

//event listeners
smartfox.addEventListener(SFSEvent.onConnection, onConnectionHandler);
smartfox.addEventListener(SFSEvent.onRoomListUpdate, onRoomListUpdateHandler);
smartfox.addEventListener(SFSEvent.onJoinRoom, onJoinRoomHandler);
smartfox.addEventListener(SFSEvent.onLogin, onLoginHandler);
smartfox.addEventListener(SFSEvent.onPublicMessage, onPublicMessageHandler);

//init the game
game._smartfox = smartfox;
game.init();

//ip and port are vars loaded from an XML file
smartfox.connect(ip, port);

function onConnectionHandler(evt:SFSEvent):void
{
	trace('connect request returned');

	if (evt.params.success)
            smartfox.login(zone, username, null);
	else
    	trace("Error connecting to the server: " + evt.params.error);

}

function onLoginHandler(evt:SFSEvent):void
{
      if (evt.params.success)
      {
      	trace("Successfully logged in as " + evt.params.name)
      	trace("Active Room: " + smartfox.activeRoomId);
      }
      else
      	trace("Zone login error; the following error occurred: " + evt.params.error)
}

function onRoomListUpdateHandler(evt:SFSEvent):void
{

      //joinRoom can only be called after the roomListUpdate
      if (smartfox.activeRoomId == -1)
            smartfox.joinRoom(room);
}

function onJoinRoomHandler(evt:SFSEvent):void
{

   if (evt.params.room)  //a room was joined
   {
       trace ("timeline onJoinRoomHandler");
	   smartfox.sendPublicMessage(username + " joined " + evt.params.room.getName());
   }
   else
   		trace("Error joining a room: " + evt.params.error);

} 

function onPublicMessageHandler(evt:SFSEvent):void
{
        trace("On Timeline user " + evt.params.sender.getName() + " said: " + evt.params.message)
}

This goes in an actionscript file:

 package com.design1online.gameinstance { 

 import it.gotoandplay.smartfoxserver.*;
 import it.gotoandplay.smartfoxserver.data.*;
 import flash.display.MovieClip; 

 public class gameinstance extends MovieClip
 {
   public var _smartfox:SmartFoxClient; 

   public function gameinstance():void
   { 

   } 

   public function init():void
   {
     _smartfox.addEventListener(SFSEvent.onJoinRoom, onJoinRoomHandler);
     _smartfox.addEventListener(SFSEvent.onPublicMessage, onPublicMessageHandler);
   } 

   function onPublicMessageHandler(evt:SFSEvent):void
   {
     trace("gameinstance user " + evt.params.sender.getName() + " said: " + evt.params.message)
   } 

   function onJoinRoomHandler(evt:SFSEvent):void
   { 

     if (evt.params.room)  //a room was joined
     {
       trace ("gameinstance onJoinRoomHandler");
       smartfox.sendPublicMessage(username + " joined " + evt.params.room.getName());
     }
     else
       trace("gameinstance error joining a room: " + evt.params.error);
   }  

   function onPublicMessageHandler(evt:SFSEvent):void
   {
     trace("gameinstance user " + evt.params.sender.getName() + " said: " + evt.params.message)
   } 

  } //end class 

 } //end package
evt.params.error

You may also like...

10 Responses

  1. sonam says:

    can u put source code.
    cause i m not getting whats dr on fla.

  2. Vortigon says:

    Does it still work?
    I get an error when trying the supposed working code, not sure if it comes from me

  3. Vortigon says:

    I launch the SFS2X server, then the swf, and I just get this in output:

    [ Send ]: sfsHttp=connect
    Error opening URL ‘http://:8080/BlueBox/HttpBox.do’
    HttpError
    connect request returned
    Error connecting to the server: I/O Error

    • Jade says:

      That means your server isn’t configured to run SFS2X correctly — it’s refusing the connection on port 8080. Make sure you have port 8080 accessible, that SFS2X is running on your server, that SFS2X was installed properly and isn’t generating any errors (check the SFS2X error log), and that your firewall isn’t restricting access to 8080 if everything else is running properly,

  4. Vortigon says:

    Alright I installed SFS PRO unstead and it works now, thanks for replies anyways ! ^^

  5. yuri says:

    i got error says “the type was not found or was not a compile-time constant:SFSEvent” what should i do?

Leave a Reply