Class SFS2X.Requests.System.PlayerToSpectatorRequest

Turns the current user from player to spectator in a Game Room.

Class Summary
Constructor Attributes Constructor Name and Description
Creates a new PlayerToSpectatorRequest instance.

Class Detail

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

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

The following example turns the current user from player to spectator in the last joined Game Room:

function someMethod()
	sfs.addEventListener(SFS2X.SFSEvent.PLAYER_TO_SPECTATOR, onPlayerToSpectatorSwitch, this);
	sfs.addEventListener(SFS2X.SFSEvent.PLAYER_TO_SPECTATOR_ERROR, onPlayerToSpectatorSwitchError, this);
	// Switch player to spectator
	sfs.send(new SFS2X.Requests.System.PlayerToSpectatorRequest());

function onPlayerToSpectatorSwitch(evtParams)
	console.log("Player " + evtParams.user + " is now a spectator");

function onPlayerToSpectatorSwitchError(evtParams)
	console.log("Unable to become a spectator due to the following error: " + evtParams.errorMessage);
{SFSRoom} targetRoom Optional, Default: null
The SFSRoom object corresponding to the Room in which the player should be turned to spectator. If null, the last Room joined by the user is used.
See also: