Class SFS2X.Requests.System.ObjectMessageRequest

Sends an object containing custom data to all users in a Room, or a subset of them.

Class Summary
Constructor Attributes Constructor Name and Description
 
SFS2X.Requests.System.ObjectMessageRequest(obj, targetRoom, recipients)
Creates a new ObjectMessageRequest instance.

Class Detail

SFS2X.Requests.System.ObjectMessageRequest(obj, targetRoom, recipients)
Creates a new ObjectMessageRequest instance. The instance must be passed to the SmartFox.send() method for the request to be performed.

This request sends an object containing custom data to the users in a Room. The object is delivered to the selected users (or all users excluding the sender) inside the target Room by means of the objectMessage event. It can be useful to send game data, like for example the target coordinates of the user's avatar in a virtual world.

The following example sends the player's avatar movement coordinates and handles the respective event (note: the myAvatar instance is supposed to be the user sprite on the stage, while the getUserAvatar method retrieves the sprite of other users' characters):

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.OBJECT_MESSAGE, onObjectMessage, this);
	
	// Send my movement to all players
	var dataObj = {};
	dataObj.x = myAvatar.x;
	dataObj.y = myAvatar.y;
	
	sfs.send(new SFS2X.Requests.System.ObjectMessageRequest(dataObj));
}

function onObjectMessage(evtParams)
{
	var dataObj = evtParams.message;
	
	var sender = evtParams.sender;
	var avatar = getUserAvatar(sender.id);
	
	avatar.x = dataObj.x;
	avatar.y = dataObj.y;
}
Parameters:
{Object} obj
An object containing custom parameters to be sent to the message recipients.
{SFSRoom} targetRoom Optional, Default: null
The SFSRoom object corresponding to the Room where the message should be dispatched; if null, the last Room joined by the user is used.
{Array} recipients Optional, Default: null
A list of SFSUser objects corresponding to the message recipients; if null, the message is sent to all users in the target Room (except the sender himself).
See also:
SFS2X.SmartFox#send
SFS2X.Entities.SFSUser
SFS2X.SFSEvent.OBJECT_MESSAGE