8.8 Tutorials: SmartFoxTris PRO (p.2)
| The source FLA of this example is found under the Examples/mx2004/pro_smartFoxTris folder. |
» Sending moves
As we said in the introduction, all the game logic is now moved on the server
side while the client will only take care of the visualization. The central
part of the client code is the extension response handler:
smartfox.onExtensionResponse = function(resObj:Object, type:String)
{
var cmd:String = resObj._cmd
switch(cmd)
{
case "start":
_global.whoseTurn = resObj.t
player1Id = resObj.p1i
player2Id = resObj.p2i
player1Name = resObj.p1n
player2Name = resObj.p2n
startGame()
break
case "stop":
_global.gameStarted = false
gamePaused(resObj.n + " left the game" + newline)
break
case "move":
moveReceived(resObj)
break
case "specStatus":
setSpectatorBoard(resObj)
break
case "tie":
_global.gameStarted = false
var win:MovieClip = showWindow("gameEnd")
win.message_txt.text = "Tie!"
break
case "win":
setWinner(resObj)
break
}
}
function startGame()
{
resetGameBoard()
hideWindow("gameEnd")
if (!iAmSpectator)
hideWindow("gameMessage")
else
hideWindow("gameSpecMessage")
_root["player1"].name.text = player1Name
_root["player2"].name.text = player2Name
setTurn()
_global.gameStarted = true
}
function moveDone(tile:MovieClip)
{
var x:Number = Number(tile._name.substr(3,1))
var y:Number = Number(tile._name.substr(5,1))
var obj:Object = {}
obj.x = x
obj.y = y
smartfox.sendXtMessage(extensionName, "move", obj)
}
function handleMove(prms, u)
{
if (gameStarted)
{
if (whoseTurn == u.getPlayerIndex())
{
var px = prms.x
var py = prms.y
if (board[py][px] == ".")
{
board[py][px] = String(u.getPlayerIndex())
whoseTurn = (whoseTurn == 1) ? 2 : 1
var o = {}
o._cmd = "move"
o.x = px
o.y = py
o.t = u.getPlayerIndex()
_server.sendResponse(o, currentRoomId, null, users)
moveCount++
checkBoard()
}
}
}
}
var response = {}
// TIE !!!
if (winner == null && moveCount == 9)
{
gameStarted = false
response._cmd = "tie"
_server.sendResponse(response, currentRoomId, null, users)
endGameResponse = response
}
else if (winner != null)
{
// There is a winner !
gameStarted = false
response._cmd = "win"
response.w = winner
_server.sendResponse(response, currentRoomId, null, users)
endGameResponse = response
}
function moveReceived(res:Object)
{
_global.whoseTurn = (res.t == 1) ? 2 : 1
setTurn()
if (res.t != _global.myID)
{
var tile:String = "sq_" + res.x + "_" + res.y
var color:String = (res.t == 1) ? "green" : "red"
setTile(tile, color)
}
}
| « previous | doc index |