Flash Challenges
From J2Play
Player can challenge his or her friends or other online players to compete against each other in a game.
The challenge will proceed as follows.
- The challenger will challenge a player to compete in the game
- The challenger will complete the first round of the challenge
- The challengee will receive a message indicating that the challenger has challenged them.
- The challengee will play their half of the turn
- Each player will continue to take turns until the challenge is completed
- The results of the challenge is displayed to the user
For this example we will look at a simple Sudoku game
For more information see Challenge API
Contents |
Initial Setup
Add these imports to the top of any file which you want to access the challenge data in (For example sudoku.as)
import ca.j2x.flash.IChallenge; import ca.j2x.flash.IController; import ca.j2x.flash.IScore; import ca.j2x.flash.Playground;
For ActionScript 3 also add
import ca.j2x.flash.lang.Global;
Then declare the controller and challenge objects
private var controller : IController; private var challengeData: IChallenge;
Finally in the constructor of your game (ex. sudoku()) initialize the controller and challenge objects
ActionScript 2
this.controller = _global.controller; challengeData=controller.getChallenge();
ActionScript 3
var j2play:IController = Global.vars.controller; challengeData=controller.getChallenge();
For convenience you may want to also add this function to your code to detect whether or not the game is in the middle of a challenge
public function isChallenge() : Boolean {
return controller.isChallenge();
}
Game State
The next step is to implement saving and loading game state. This is only required if it is important that the state of the game is consistent between plays. For example in sudoku it is important for fairness that each player plays on the same board
Load State
To load the state from the challenge object first wrap you game loading function in the following code:
var canLoad = loadChallengeState();
if(!canLoad) {
//Load normally
}
Then implement a loadChallengeState function which uses Challenge.getCustomInformation() to load any data this game has saved in the state. Challenge state is saved as a simple key value pair.
Here is an example loadChallengeState function from sudoku which loads the day the board is for, the difficulty of the board, and a comma seperated list of values repersenting each individual cell of the board. Notice how it checks for null to see if the challenge state exists in the database
function loadChallengeState():Boolean {
if(isChallenge()) {
//Try and load state from db
var tiles = challengeData.getCustomInformation('tiles',-1);
var day = challengeData.getCustomInformation('day',-1);
var difficulty = challengeData.getCustomInformation('difficulty',-1);
if (tiles!=null && day != null && difficulty != null)
{
this.settings.sDifficulty = difficulty; //Set local state variables
gameLoaded(tiles.split(","), Number(day), 1); //Load the game with your load function
return true;
}
}
return false;
}
Save State
To save the state of the game use Challenge.setCustomInformation() to save game state in the challenge object. Challenge data is stored as a key value pair (ie difficulty->"Easy")
Here is an example saveChallengeState from the sudoku. It saves three values: the day, the difficulty, a list of tiles to the challenge object
function saveChallengeState():Void {
if(isChallenge()) {
challengeData.setCustomInformation('tiles',-1, this.aLoadedTiles);
challengeData.setCustomInformation('day',-1, this.iDay);
challengeData.setCustomInformation('difficulty',-1, this.settings.sDifficulty);
}
}
Start Turn
When the player starts playing their half of a challenge the game updates their status to playing by calling Challenge.updateGamePlay()
if(isChallenge()) {
challengeData.updateGamePlay();
}
End Turn
When the user's turn is completed their are 5 things that the game must do
- Compute Results
- Save State (See above)
- Move to next player
- Show results
- Send the onGameOver event
Example from sudoku
if(isChallenge()) {
computeChallengeResult(iSeconds, this.settings.sDifficulty ); //Compute the results
saveChallengeState(); //Save state
challengeData.updateNextTurn(1-challengeData.getMyIndex(),true); //End my turn and specify that this is my last turn
Playground.showChallengeResult(challengeData.getId()); //Use built in result screen
Playground.sendGameOverEvent(); //Send onGameOver event
}
Compute Results
This is the main function which computes the result of the challenge. It preforms the following functions
- Sets the challenge score
- Sets the result of the challenge if the challenge is completed
- Sets the display message to be displayed
Here is the computeChallengeResult function from sudoku:
function computeChallengeResult(challengeeScore:Number, difficulty:String) : Void {
//Compute score type
var scoreType:String = "timeeasy";
if (difficulty == "Medium") {
scoreType= "timemedium";
} else if (difficulty == "Hard") {
scoreType = "timehard";
}
//Send score
challengeData.setScore(challengeeScore * 1000, scoreType, challengeData.getMyIndex());
if (challengeData.getMyIndex()==0) { // Signifies one half of the challenge
challengeData.setDisplayMessage(1,"Score to beat: " + challengeeScore + " on " + difficulty);
} else {
var challengerScore =challengeData.getScore(0) / 1000;
if (challengeeScore>challengerScore) {// I Lost
//Win Lost Tie
challengeData.setResult(Array(Object(0)),Array(Object(1)),Array());
challengeData.setDisplayMessages(Array(
"You won the Daily Sudoku challenge against "+challengeData.getNickname(1),
"You lost the Daily Sudoku challenge to "+challengeData.getNickname(0)));
} else if (challengeeScore<=challengerScore) { // I Won
challengeData.setResult(Array(Object(1)),Array(Object(0)),Array());
challengeData.setDisplayMessages(Array(
"You lost the Daily Sudoku challenge to "+challengeData.getNickname(1),
"You won the Daily Sudoku challenge against "+challengeData.getNickname(0)));
}
}
}
Set Score
Send the score the user got using Challenge.setScore() . The params this function takes are the score to set, the score type of the score, and the index of the use. Make sure to use the same score type here as you post to the leaderboard
//Compute scoreType
var scoreType:String = "timeeasy";
if (difficulty == "Medium") {
scoreType= "timemedium";
} else if (difficulty == "Hard") {
scoreType = "timehard";
}
//Set the score
challengeData.setScore(challengeeScore * 1000 , scoreType, challengeData.getMyIndex());
Challenge Result
If the challenge is complete set the results for each user using Challenge.setResult() . This function takes three arrays. The array of people's index who won, the array of people's index who lost, and the array of people's index who tied
challengeData.setResult(Array(Object(0)),Array(Object(1)),Array());
Display Message
Set the message sent to the user using Challenge.setDisplayMessages() . This function takes an array of messages, so message at element 0 will be associated player at index 0
challengeData.setDisplayMessages(Array( "You lost the Daily Sudoku challenge to "+challengeData.getNickname(1), "You won the Daily Sudoku challenge against "+challengeData.getNickname(0)));
Save State
See above
saveChallengeState();
Move to Next Player
Once all of the data is stored in the challenge object call Challenge.updateNextTurn() with the index of the player whose turn is next (in two player games this should be 1-myIndex) and the whether or not this is the user's last turn (true means that it is false means that it isn't)
challengeData.updateNextTurn(1-challengeData.getMyIndex(),true);
Show Results
Show the results to the use. To use the built in result GUI elements call Playground.showChallengeResult() passing in the id of the challenge objcet
Playground.showChallengeResult(challengeData.getId());
Game Over
Once all this is completed signal the onGameOver event by calling Playground.sendGameOverEvent()
Playground.sendGameOverEvent();
Other function for challenge information
You can access all the challenge information through interface ca.j2x.flash.IChallenge.
- Challenge.getPlayerNumber()
- Challenge.getMyIndex()
- Challenge.getUsername()
- Challenge.getNickname()
- Challenge.getPlayerStatus()
- Challenge.getPlayerPendingStatus()
- Challenge.getDisplayMessage()
- Challenge.getResult()
- Challenge.getCustomInformation()
- Challenge.setCustomInformation()
For a complete list of available functions in the Challenge object see Challenge API
