阅读新闻阅读所有新闻

FLASH网游通过XMLSocket与VB后台通信教程 (4)

接下来是在有人物的FLASH里加入与服务器通讯的功能了。我在FLASH的AS里加入XMLSocket的代码:

//数据链接
var conects:String = "127.0.0.1";
var socket:XMLSocket = new XMLSocket();
socket.connect(conects, 9999);
this.socket.onData = function(str:String) {
var type:String = str.substr(0, 2);
var tmparr:Array = str.substr(2).split("&");
//所有在线角色和位置
if (type == "00") {
  if (tmparr.length>1) {
   for (var i:Number = 0; i<tmparr.length; i += 3) {
    tmpName = tmparr[i].split("=");
    tmpx = tmparr[(i+1)].split("=");
    tmpy = tmparr[(i+2)].split("=");
    allRoles[allRoles.length] = createRole(tmpName[1], false, 5, Number(tmpx[1]), Number(tmpy[1]));
   }
  }
}
//有新用户登陆
if (type == "01") {
  tmpName = tmparr[0].split("=");
  allRoles[allRoles.length] = createRole(tmpName[1], false, 5, Stage.width/2, Stage.height/2);
}
//有用户移动
if (type == "02") {
  tmpName = tmparr[0].split("=");
  tmpx = tmparr[1].split("=");
  tmpy = tmparr[2].split("=");
  for (var i = 0; i<allRoles.length; i++) {
   if (allRoles[i].id == tmpName[1]) {
    moveRole(allRoles[i], Number(tmpx[1]), Number(tmpy[1]));
    break;
   }
  }
}
//用户下线
if (type == "03") {
  tmpName = tmparr[0].split("=");
  for (var i = 0; i<allRoles.length; i++) {
   if (allRoles[i].id == tmpName[1]) {
    trace(allRoles[i])
    removeMovieClip(allRoles[i]);
    allRoles[i].splice(i, 1);
    break;
   }
  }
}
};

当然,原来的代码里还要加上一些东西,比如在创造角色的时候,要判断是否是自己控制的角色,是的话就要告诉服务器我登陆了。比如在鼠标点击的时候,也要告诉服务器我移动了。下边我把完整的AS代码再贴一遍。

//数据链接
var conects:String = "127.0.0.1";
var socket:XMLSocket = new XMLSocket();
socket.connect(conects, 9999);
this.socket.onData = function(str:String) {
var type:String = str.substr(0, 2);
var tmparr:Array = str.substr(2).split("&");
if (type == "00") {
  if (tmparr.length>1) {
   for (var i:Number = 0; i<tmparr.length; i += 3) {
    tmpName = tmparr[i].split("=");
    tmpx = tmparr[(i+1)].split("=");
    tmpy = tmparr[(i+2)].split("=");
    allRoles[allRoles.length] = createRole(tmpName[1], false, 5, Number(tmpx[1]), Number(tmpy[1]));
   }
  }
}
if (type == "01") {
  tmpName = tmparr[0].split("=");
  allRoles[allRoles.length] = createRole(tmpName[1], false, 5, Stage.width/2, Stage.height/2);
}
if (type == "02") {
  tmpName = tmparr[0].split("=");
  tmpx = tmparr[1].split("=");
  tmpy = tmparr[2].split("=");
  for (var i = 0; i<allRoles.length; i++) {
   if (allRoles[i].id == tmpName[1]) {
    moveRole(allRoles[i], Number(tmpx[1]), Number(tmpy[1]));
    break;
   }
  }
}
if (type == "03") {
  tmpName = tmparr[0].split("=");
  for (var i = 0; i<allRoles.length; i++) {
   if (allRoles[i].id == tmpName[1]) {
    trace(allRoles[i]);
    removeMovieClip(allRoles[i]);
    allRoles[i].splice(i, 1);
    break;
   }
  }
}
};
//创建鼠标监听器
var mouseListener:Object = new Object();
mouseListener.onMouseDown = function() {
for (var i = 0; i<allRoles.length; i++) {
  if (allRoles[i].ctrl) {
   moveRole(allRoles[i], _xmouse, _ymouse);
   socket.send("02u="+allRoles[i].id+"&x="+_xmouse+"&y="+_ymouse);
   break;
  }
}
};
Mouse.addListener(mouseListener);
//创建角色
var allRoles:Array = new Array();
allRoles[allRoles.length] = createRole(Math.random(), true, 5, Stage.width/2, Stage.height/2);
function createRole() {
var roleObj:MovieClip = this.attachMovie("role", "role"+allRoles.length+"_mc", this.getNextHighestDepth());
roleObj.id = arguments[0];
roleObj.ctrl = arguments[1];
roleObj.speed = arguments[2];
roleObj._x = arguments[3];
roleObj._y = arguments[4];
roleObj.x = roleObj._x;
roleObj.y = roleObj._y;
//如果是创造了玩家自己,就要向告诉服务器“我登陆了”
if (arguments[1]) {
  socket.send("01u="+arguments[0]);
}
return roleObj;
}
//角色移动
function moveRole(role:MovieClip, x:Number, y:Number) {
role.x = x;
role.y = y;
role.angle = Math.atan2(role.y-role._y, role.x-role._x);
role.dire = 1+Math.round((role.angle+Math.PI)/(Math.PI/4));
role.dire = role.dire>6 ? role.dire-6 : role.dire+2;
if (role.dire>5) {
  role.dire = role.dire == 6 ? 4 : role.dire == 7 ? 3 : 2;
  role._xscale = -100;
} else {
  role._xscale = 100;
}
role.gotoAndPlay("run_"+role.dire);
role.sta;
role.onEnterFrame = function() {
  if (this.x != this._x) {
   this._x += Math.abs(this.x-this._x)>Math.abs(this.speed*Math.cos(this.angle)) ? this.speed*Math.cos(this.angle) : this.x-this._x;
  }
  if (this.y != this._y) {
   this._y += Math.abs(this.y-this._y)>Math.abs(this.speed*Math.sin(this.angle)) ? this.speed*Math.sin(this.angle) : this.y-this._y;
  }
  if (this.x == this._x && this.y == this._y) {
   delete this.onEnterFrame;
   this.gotoAndStop("stand_"+this.dire);
  }
};
}

以下是做好的所有文件,包括FLASH源文件和VB源文件。
附件:  game.rar

忘了说了:因为后台是用VS2003写的,所以在没有装VS的电脑上好象运行不了服务器。这个教程写了好几天,终于完成了。原来还想把聊天功能,换头发功能都做上去的,懒了...改天有时间想做个完整的游戏出来玩玩...

添加时间2007-7-18 23:18:58 共被阅读:5501 次
类别索引