new SpectatorToPlayerRequest([targetRoom])

Creates a new SpectatorToPlayerRequest instance. The instance must be passed to the SmartFox.send() method for the request to be executed.

This request turns the current user from spectator to player in a Game Room. If the operation is successful, all the users in the target Room are notified with the spectatorToPlayer event. The operation could fail if no player slots are available in the Game Room at the time of the request; in this case the spectatorToPlayerError event is dispatched to the requester's client.

Example

This example turns the current user from spectator to player in the last joined Game Room.

function someMethod()
{
	sfs.addEventListener(SFS2X.SFSEvent.SPECTATOR_TO_PLAYER, onSpectatorToPlayerSwitch, this);
	sfs.addEventListener(SFS2X.SFSEvent.SPECTATOR_TO_PLAYER_ERROR, onSpectatorToPlayerSwitchError, this);

	// Switch spectator to player
	sfs.send(new SFS2X.SpectatorToPlayerRequest());
}

function onSpectatorToPlayerSwitch(evtParams)
{
	console.log("Spectator " + evtParams.user + " is now a player");
}

function onSpectatorToPlayerSwitchError(evtParams)
{
	console.log("Unable to become a player due to the following error: " + evtParams.errorMessage);
}

Parameter

Name Type Optional Description

targetRoom

 

Yes

The SFSRoom object corresponding to the Room in which the spectator should be turned to player. If null, the last Room joined by the user is used.

Defaults to null.

See also
SmartFox#send
SFSEvent.SPECTATOR_TO_PLAYER
SFSEvent.SPECTATOR_TO_PLAYER_ERROR
PlayerToSpectatorRequest