new PlayerToSpectatorRequest([targetRoom])

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

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.

Example

This 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.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);
}

Parameter

Name Type Optional Description

targetRoom

 

Yes

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.

Defaults to null.

See also
SmartFox#send
SFSEvent.PLAYER_TO_SPECTATOR
SFSEvent.PLAYER_TO_SPECTATOR_ERROR
SpectatorToPlayerRequest