8.6 Tutorials: Realtime maze (part 2)
» Player movements and updates
The "start" event is sent from the server extensions using this code:
function startGame()
{
gameStarted = true
var res = {}
res._cmd = "start"
res.p1 = {id:p1id, name:users[p1id].getName(), x:1, y:1}
res.p2 = {id:p2id, name:users[p2id].getName(), x:22, y:10}
_server.sendResponse(res, currentRoomId, null, users)
}
if (protocol == "xml")
{
switch(cmd)
{
case "start":
player1Id = resObj.p1.id
player2Id = resObj.p2.id
player1Name = resObj.p1.name
player2Name = resObj.p2.name
myX = resObj["p" + _global.myID].x
myY = resObj["p" + _global.myID].y
_global.opID = _global.myID == 1 ? 2 : 1
opX = resObj["p" + _global.opID].x
opY = resObj["p" + _global.opID].y
startGame()
break
case "stop":
_global.gameStarted = false
delete this.onEnterFrame
gamePaused(resObj.n + " left the game" + newline)
break
}
}
function mainThread()
{
if (_global.gameStarted)
{
if(!mySprite.moving)
{
if(Key.isDown(Key.LEFT) && obstacles.indexOf(map[mySprite.py][mySprite.px - 1]) == -1)
{
sendMyMove(mySprite.px-1, mySprite.py)
moveByTime(mySprite, mySprite.px-1, mySprite.py, playerSpeed)
}
else if(Key.isDown(Key.RIGHT) && obstacles.indexOf(map[mySprite.py][mySprite.px + 1]) == -1)
{
sendMyMove(mySprite.px+1, mySprite.py)
moveByTime(mySprite, mySprite.px+1, mySprite.py, playerSpeed)
}
else if(Key.isDown(Key.UP) && obstacles.indexOf(map[mySprite.py - 1][mySprite.px]) == -1)
{
sendMyMove(mySprite.px, mySprite.py-1)
moveByTime(mySprite, mySprite.px, mySprite.py-1, playerSpeed)
}
else if(Key.isDown(Key.DOWN) && obstacles.indexOf(map[mySprite.py + 1][mySprite.px]) == -1)
{
sendMyMove(mySprite.px, mySprite.py+1)
moveByTime(mySprite, mySprite.px, mySprite.py+1, playerSpeed)
}
}
// If the moves queue of the opponent contains data and the opponent is not
// being animated we update its position
if(!opSprite.moving && opSprite.moves.length > 0)
{
moveByTime(opSprite, opSprite.moves[0].px, opSprite.moves[0].py, playerSpeed)
}
}
}
function moveByTime(who, px, py, duration)
{
who.moving = true
if(who.moves.length > 1)
{
who._x = who.moves[who.moves.length - 2].px *tileSize
who._y = who.moves[who.moves.length - 2].py *tileSize
px = who.moves[who.moves.length - 1].px
py = who.moves[who.moves.length - 1].py
}
who.moves = []
var sx, sy, ex, ey
sx = who._x
sy = who._y
ex = px * tileSize
ey = py * tileSize
who.ani_startTime = getTimer()
who.ani_endTime = who.ani_startTime + duration
who.duration = duration
who.sx = sx
who.sy = sy
who.dx = ex - sx
who.dy = ey - sy
who.onEnterFrame = animateByTime
}
function sendMyMove(px:Number, py:Number)
{
var o = []
o.push(px)
o.push(py)
smartfox.sendXtMessage(extensionName, "mv", o, "str")
}
function handleRequest(cmd, params, user, fromRoom, protocol)
{
if (protocol == "str")
{
switch(cmd)
{
case "mv":
handleMove(params, user)
break
}
}
}
function handleMove(params, user)
{
if (gameStarted)
{
var res = [] // The list of params
res[0] = "mv" // at index = 0, we store the command name
res.push(params[0]) // this is the X pos of the player
res.push(params[1]) // this is the Y pos of the player
// Chose the recipient
// We send this message only to the other client
var uid = user.getUserId()
var recipient = (uid == p1id) ? users[p2id]:users[p1id]
_server.sendResponse(res, currentRoomId, user, [recipient], "str")
}
}
smartfox.onExtensionResponse = function(resObj:Object, protocol:String)
{
var cmd:String = resObj._cmd
if (protocol == "xml")
{
// ...
}
else if (protocol == "str")
{
var cmd = resObj[0] // command name
var rid = Number(resObj[1]) // roomId
switch(cmd)
{
case "mv":
handleOpponentMove(Number(resObj[2]), Number(resObj[3]))
break
}
}
}
function handleOpponentMove(x:Number, y:Number)
{
if (opSprite.moves == undefined)
opSprite.moves = []
opSprite.moves.push({px:x, py:y})
}
| « previous | doc index |