smartfox events

Need help with SmartFoxServer? You didn't find an answer in our documentation? Please, post your questions here!

Moderators: Lapo, Bax

Post Reply
rik.schennink
Posts: 21
Joined: 28 Mar 2006, 15:08
Location: The Netherlands

smartfox events

Post by rik.schennink »

I was wondering, i created a class to handle all the smartfox things happening.. i noticed that i can use.. smartfox.onLogin.. smartFox.onJoinRoom.. and so on..

When i create a class and want to listen to events originating from the class i cannot do it like u guys do..

suppose i have a class named Game and in that class i fire the event onBegin.. i create an instance of Game and call it game.

i cannot use game.onBegin to listened to the event, it just doesnt work.. i have to write three lines of code to do just that.

Code: Select all

listener = {};
listener.onBegin = Delegate.create(this,onBegin);
game.addEventListener("onBegin",listener);
now i checked your smartfox client classes source code but could not find the solution for this problem.. Can u give me a hint or a small tutorial on how to be able to use game.OnBegin instead of the three lines of code i need to use now?
rikki-tikki
Posts: 4
Joined: 13 Apr 2006, 11:51
Location: Novosibirsk, Russia
Contact:

Re: smartfox events

Post by rikki-tikki »

as far, as i can understand source code of smartfoxserver flash api, it does not support mx.events.EventDispatcher, so you can't use addEventListener method directly to api.
class SmartFoxClient have some abstract functions (onLogin, onRoomUpdate and so on), but this functions does not dispatch any events.

i see two ways to resolve this problem
1) you can modify smartfoxserver flash api and add any events you need. imho this is a not right way, but it will be work.

2) you can create your own proxy-class, what will be catch event from instance of SmartFoxClient and dispatch this events. something like this...

Code: Select all

import mx.utils.Delegate;
import mx.events.EventDispatcher;
import it.gotoandplay.smartfoxserver.SmartFoxClient;
class smartProxy {
	public var smartServer : SmartFoxClient;
	public var addEventListener : Function;
	public var removeEventListener : Function;
	private var dispatchEvent : Function;
	
	function smartProxy() :Void {
		EventDispatcher.initialize(this);

		smartServer = new SmartFoxClient();
		smartServer.onSomeEvent = Delegate.create (this, someEvent);

	}

	private function someEvent():Void {
		var evtObj :Object = new Object ();
		evtObj.target = smartServer;
		evtObj.type = "someEvent";

		dispatchEvent(evtObj);
	}
}
i write this class directly in browser, so it can have some errors :)
so, now you can create instance of this class instead instance of SmartFoxClient and use it public methods "addEventListener" and "removeEventListener"

p.s. forgive me for bad English, it is not my native language.
User avatar
Lapo
Site Admin
Posts: 23438
Joined: 21 Mar 2005, 09:50
Location: Italy

Post by Lapo »

Hi rik.schennink,
we didn't use the EventDispatcher technique because we did a couple of performance tests in MX2004 and we found that the EventDispatcher is significantly slower than our method. Maybe with the latest flash player things are getting better.

The SFS events are declared inside the class as "virtual" or "abstract" (as rikki-tikki pointed out) methods.
This means that you just need to delegate those events to your methods.

We'll use the internal event dispathcing system in the AS 3.0 API
now i checked your smartfox client classes source code but could not find the solution for this problem.. Can u give me a hint or a small tutorial on how to be able to use game.OnBegin instead of the three lines of code i need to use now?
Yes, here it is:

Code: Select all

class MyGame
{
	public var score:Number
	public var onGameStart:Function
	public var onGameEnd:Function
	
	function MyGame()
	{
		this.score = 0
	}
	
	public function start()
	{
		trace("Game starts")
		this.onGameStart()
	}
	
	public function end()
	{
		trace("Game stops")
		this.onGameEnd()
	}
}
and here's how to use the class:

Code: Select all

var game:MyGame = new MyGame()
game.onGameStart = gameStart
game.onGameEnd = gameEnd

function gameStart()
{
	// Handle event
}

function gameEnd()
{
	// Handle event
}
hope it helps :)
Lapo
--
gotoAndPlay()
...addicted to flash games
rik.schennink
Posts: 21
Joined: 28 Mar 2006, 15:08
Location: The Netherlands

Post by rik.schennink »

Thx for the small tutorial :D I now totally understand :)
Post Reply