论坛交流
首页办公自动化| 网页制作| 平面设计| 动画制作| 数据库开发| 程序设计| 全部视频教程
应用视频: Windows | Word2007 | Excel2007 | PowerPoint2007 | Dreamweaver 8 | Fireworks 8 | Flash 8 | Photoshop cs | CorelDraw 12
编程视频: C语言视频教程 | HTML | Div+Css布局 | Javascript | Access数据库 | Asp | Sql Server数据库Asp.net  | Flash AS
当前位置 > 文字教程 > Flash AS编程教程
Tag:2.0,3.0菜鸟,游戏,,cs,技巧,源码,,文本,文字,函数,音乐,随机,拖拽,asp,access,xml,mc,视频教程

连网5子棋

文章类别:Flash AS编程 | 发表日期:2008-10-6 17:35:24


终于把连网5子棋也做好了~~高兴一下下  ^ ^
在做这个五子棋时发现 so是很耗资源的一个东东呢`~所以也顺便把前两天做好的聊天室中用so来广播的方式也改过来了`~

虽然脚本很多细节都还没写~~但为了庆祝下~~决定把脚本也放上来一下~~
client段第一帧:
////////////////////////在线人数& 更新在线用户/////////////////
stop();
menber = new Array ();
menberlength = menber.length;
temp_mb = new Array ();
var client_nc = new NetConnection ();
btn.onPress = function () {
 if (_root.id.text != "") {
  ID = _root.id.text;
  _root.ID_name = ID;
  id.text = "";
  pass_num = "123";
  client_nc.connect ("rtmp://localhost/8.2", ID, pass_num);
  //users_so = SharedObject.getRemote ('users_so', client_nc.uri, false);
  //users_so.connect (client_nc);
  //client_nc.call("msgfromclient", resObj, "第一个call");
  client_nc.updata = function (list) {
   _root.mylist.labels = list;
   gotoAndStop (2);
  };
  /*
  users_so.onSync = function (list) {
  _root.mylist.labels = users_so.data.users;
  menber = users_so.data.users;
  
  };
  */
 }
};
//////////////////////////////第2帧/////////////////////////////////////////////
stop ();
//temp_mb=menber.slice(0);
txt = this.createTextField ("my_txt", this.getNextHighestDepth (), 485, 50, 150, 300);
my_txt.border = true;
my_txt.multiline = true;
my_txt.wordWrap = true;
my_txt.size = 12;
Name.text = _root.ID_name;
mync2 = new NetConnection ();
mync2.connect ("rtmp://localhost/8888.2");
mync2.call ("Msg_T_client", null, ID);
mync2.onStatus = function (info) {
 trace (info.code);
 if (info.code == "NetConnection.Connect.Success") {
  trace ("接通");
 }
};
//so = SharedObject.getRemote ('users_so', mync2.uri, false);
//so.connect (mync2);
/*
so.onSync = function (list) {
if (so.data.txt == undefined) {
}
else {
_root.my_txt.text += so.data.txt;
_root.my_txt.scroll++;
so.data.txt = "";
}
};
*/
sendtxt.onPress = function () {
 tmp = [_root.ID_name, input2.text];
 //so.data.txt = _root.ID_name + "说:" + input2.text + newline;
 mync2.call ("sendMsg", null, tmp);
 input2.text = "";
};
mync2.w1 = function (what) {
 trace (what);
 _root.my_txt.text += what[0] + "说:" + what[1] + newline;
 _root.my_txt.scroll++;
};
mync2.handlderName = function (what) {
 if (what != _root.ID_name) {
  _root.my_txt.text += what + "-- 进来了" + newline;
  _root.my_txt.scroll++;
 }
};
Clear.onPress = function () {
 _root.my_txt.text = "";
};
////////////////////////////////////////////////////////////
tmp = new Array ();
棋盘 = new Array ();
白棋 = new Array ();
黑棋 = new Array ();
_root.Depths = 0;
for (var i = 0; i < 11; i++) {
 for (var j = 10; j >= 1; j--) {
  棋盘.push ([0, String ((i + 1) + "" + (j + 1)), 0, (i + 1), (j + 1), 0]);
 }
}
this.createEmptyMovieClip ("格局", -1);
for (var i = 1; i <= 11; i++) {
 for (var j = 1; j <= 11; j++) {
  格子 = 格局.attachMovie ("fram", "格子" + ((i - 1) * 11 + j), i * 11 + j);
  格子._x = 40 * i + 20;
  格子._y = 40 * j + 20;
  tmp.push ([格子, 格子._x, 格子._y]);
 }
}
this.createEmptyMovieClip ("棋盘_mc", 156);
game.onPress = function () {
 _root.ingame = true;
 mync2.call ("start_game", null, ID);
 this.enabled = false;
};
mync2.fullman = function () {
 trace ("人数满了");
 game.enabled = false;
};
mync2.re_show = function (what) {
 //接收对方的信息;
 trace ("接收对方的信息=" + what);
 _root.ingame = true;
 _root.Depths++;
 trace ("深度=" + _root.Depths);
 棋 = _root.棋盘_mc.attachMovie ("棋", "棋" + _root.Depths, 1696 + _root.Depths);
 棋._x = what[0][0];
 棋._y = what[0][1];
 if (what[1] == 0) {
  type = 2;
  checkd (棋盘, "白棋", 1, 棋, type);
  for (var i = 0; i < tmp.length; i++) {
   if (tmp[i][0]._name == what[0][2]) {
    tmp[i][0].blo = false;
   }
  }
 }
 if (what[1] == 1) {
  type = 3;
  checkd (棋盘, "黑棋", 1, 棋, type);
  for (var i = 0; i < tmp.length; i++) {
   if (tmp[i][0]._name == what[0][2]) {
    tmp[i][0].blo = false;
   }
  }
  //trace("棋盘"+棋盘)
  //checkd (棋盘, "黑棋", 1, 棋);
 }
 棋.gotoAndStop (type);
};
mync2.QXQ = function (what) {
 trace ("list=" + what);
 _root.ingame = true;
};
mync2.winMsg = function (who) {
 trace (who + "===================vvvvvvvvvvvvwin");
 _root.ingame = false;
 _root.my_txt.text += "恭喜" + who + "赢了!!";
};
mync2.full_start = function (list) {
 trace ("list=" + list);
 for (var i = 0; i < list.length; i++) {
  if (_root.ID_name == list[i][0] && list[i][2] == 0) {
   hei.text = "黑棋: " + list[i][0];
   _root.ingame = true;
   if (i == 0) {
    init (3, list[1]);
   }
   else if (i == 1) {
    init (3, list[0]);
   }
  }
  else if (_root.ID_name == list[i][0] && list[i][2] == 1) {
   bai.text = "白棋: " + list[i][0];
   _root.ingame = false;
   if (i == 0) {
    init (2, list[1]);
   }
   else if (i == 1) {
    init (2, list[0]);
   }
  }
 }
};
function init (type, arr) {
 onMouseDown = function () {
  if (_root.ingame) {
   _root.point._x = _xmouse;
   _root.point._y = _ymouse;
   for (var i = 0; i < tmp.length; i++) {
    if (_root.point.hitTest (tmp[i][0].hit) && tmp[i][0].blo == undefined && _xmouse >= 20 && _xmouse <= 480 && _ymouse >= 20 && _ymouse <= 480) {
     _root.Depths++;
     trace ("深度=" + _root.Depths);
     棋 = _root.棋盘_mc.attachMovie ("棋", "棋" + _root.Depths, 1696 + _root.Depths);
     trace ("自己=" + 棋 + "+++++");
     棋.gotoAndStop (type);
     棋._x = tmp[i][0]._x;
     棋._y = tmp[i][0]._y;
     tmp[i][0].blo = false;
     send_blo = tmp[i][0]._name;
     trace (send_blo + "send_blo");
     if (type == 2) {
      trace ("白棋");
      checkd (棋盘, "白棋", 1, 棋, "disNess");
      worl (白棋[白棋.length - 1][1], 白棋[白棋.length - 1][2], 白棋, 黑棋, "白棋");
     }
     else if (type == 3) {
      trace ("黑棋");
      checkd (棋盘, "黑棋", 1, 棋, "disNess");
      worl (黑棋[黑棋.length - 1][1], 黑棋[黑棋.length - 1][2], 黑棋, 白棋, "黑棋");
     }
     send_tmp = [tmp[i][0]._x, tmp[i][0]._y, send_blo];
     arr[3] = send_tmp;
     //arr[4]=
     trace ("发送服务器=" + arr);
     mync2.call ("stopFor", null, arr);
     _root.ingame = false;
    }
   }
  }
 };
}
function checkd (棋盘:Array, type, num, who, ro) {
 for (var i = 0; i < 棋盘.length; i++) {
  if (棋盘[i][1] == (棋._x - 20) / 40 + "" + (12 - (棋._y - 20) / 40)) {
   棋盘[i][0] = type;
   棋盘[i][2] = num;
   if (2 + _root.Depths % 2 == 2 || int (ro) == 2) {
    白棋.push ([棋盘[i][1], 棋盘[i][3], 棋盘[i][4], who]);
    trace ("白棋=" + 白棋);
   }
   else if (2 + _root.Depths % 2 == 3 || ro == 3) {
    黑棋.push ([棋盘[i][1], 棋盘[i][3], 棋盘[i][4], who]);
    trace ("黑棋=" + 黑棋);
   }
   break;
  }
 }
}
function worl (x, y, arr, arr2, obj) {
 var empty = [];
 var temp = [];
 var temp2 = [];
 var tempy = [];
 var tempx = [];
 var tmp_y = y;
 var tmp_x = x;
 var tmp_x2 = x;
 var tmp_y2 = y;
 //  检查“/”
 for (var i = x - 1; i >= 1; i--) {
  tmp_y--;
  if (tmp_y < 1) {
   break;
  }
  obj1 = i + "" + tmp_y;
  temp.push ([obj1, 0]);
 }
 temp.reverse ();
 temp.push ([arr[arr.length - 1][0], 0]);
 for (var j = y + 1; j <= 11; j++) {
  tmp_x++;
  obj2 = tmp_x + "" + j;
  temp.push ([obj2, 0]);
 }
 //检查"\"
 for (var i = y - 1; i >= 1; i--) {
  tmp_x2++;
  if (tmp_x2 > 11) {
   break;
  }
  obj1 = tmp_x2 + "" + i;
  temp2.push ([obj1, 0]);
 }
 temp2.reverse ();
 temp2.push ([arr[arr.length - 1][0], 0]);
 for (var j = x - 1; j >= 1; j--) {
  tmp_y2++;
  obj2 = j + "" + tmp_y2;
  temp2.push ([obj2, 0]);
 }
 //检查“--”“|”
 for (var i = 1; i <= 11; i++) {
  t_x = x + "" + i;
  t_y = i + "" + y;
  tempy.push ([t_x, 0]);
  tempx.push ([t_y, 0]);
 }
 检查 (arr, temp);
 检查 (arr, temp2);
 检查 (arr, tempx);
 检查 (arr, tempy);
 检查 (arr2, temp, 2);
 检查 (arr2, temp2, 2);
 检查 (arr2, tempx, 2);
 检查 (arr2, tempy, 2);
 判断 (temp, obj);
 判断 (temp2, obj);
 判断 (tempx, obj);
 判断 (tempy, obj);
}
function 检查 (arr, temp, type) {
 for (var i = 0; i < arr.length; i++) {
  for (var j = 0; j < temp.length; j++) {
   if (temp[j][0] == arr[i][0]) {
    if (type != undefined) {
     temp[j][1] = 2;
    }
    else {
     temp[j][1] = 1;
    }
    continue;
   }
  }
 }
}
function 判断 (arr, obj) {
 if (arr.length >= 5) {
  for (var i = 0; i < arr.length; i++) {
   if (arr[i][1] == 1 && arr[i + 1][1] == 1 && arr[i + 2][1] == 1 && arr[i + 3][1] == 1 && arr[i + 4][1] == 1) {
    if (obj == "黑棋") {
     trace ("黑棋赢了");
     mync2.call ("overGame", null, _root.ID_name);
     //_root.gotoAndStop ("win");
     _root.winer = "黑棋";
    }
    else {
     //_root.gotoAndStop ("win");
     _root.winer = "白棋";
     trace ("白棋赢了");
     mync2.call ("overGame", null, _root.ID_name);
    }
    break;
   }
  }
 }
}
//////////////////////////////////////////////////////////////////////////////////////////////


服务端
rtmp://localhost/8888.2
的main.as文件:
application.onAppStart = function () {
 //this.so = SharedObject.get ('so');
 count = 0;
 in_player = [];
 in_game = [0, 0];
 write = [];
 black = [];
};
application.onConnect = function (client) {
 application.acceptConnection (client);
};
Client.prototype.Msg_T_client = function (what) {
 trace (what + "服务器呼叫成功");
 /*
 for(var i=0;i<application.clients.length;i++){
 trace(application.clients[i])
 application.clients[i].call("handlderName",null,what)
 }*/
 application.broadcastMsg ("handlderName", what);
 return;
};
Client.prototype.sendMsg = function (what) {
 application.broadcastMsg ("w1", what);
 return;
};
////////////////////////////////////////////////////////////////
//响应client端
Client.prototype.start_game = function (who) {
 if (count < 2) {
  in_player.push (who);
  in_game[count] = [who, this, count];
  trace ("in_player=" + in_player);
  trace ("in_game=" + in_game);
  count++;
  //当游戏人数满时执行;
  if (count == 2) {
   trace ("fullman");
   application.broadcastMsg ("fullman", who);
   for (var i = 0; i < 2; i++) {
    //呼叫开始游戏的client端;
    trace (in_game[i]);
    in_game[i][1].call ("full_start", null, in_game);
   }
  }
 }
};
Client.prototype.overGame=function(who){
 trace(who+"================")
 application.broadcastMsg ("winMsg", who);
 
 }
/*
Client.prototype.stopFor = function (num) {
trace(num[1]+"+++++++++++")
num[1].call("请下棋",null,1)
};
*/
Client.prototype.stopFor = function (num) {
 //num[2]对方
 if(arr=undefined){arr="没找到"}
 if (num[2] == 0) {
  trace (" 白棋发送给服务器=" + num);
 }
 if (num[2] == 1) {
  trace ("黑棋发送给服务器=" + num);
 }
 //当对方是黑棋时,整理自己的数组信息
 if (num[2] == 0) {
  write.push (num[num.length - 1]);
  if (write[0] != undefined) {
   arr = [write[write.length - 1], 0];
  }
  trace ("to 黑棋=" + arr);
 }
 //当对方是白其时 ,整理自己的数组信息
 if (num[2] == 1) {
  black.push (num[num.length - 1]);
  if (black[0] != undefined) {
   arr = [black[black.length - 1], 1];
  }
  trace ("to 白棋=" + arr);
 }
 
 for (var i = 0; i < in_game.length; i++) {
  if (num[0] == in_game[i][0]) {
   //in_game[i][1].call ("QXQ", null, 1);
   //trace (in_game[i][0] + "callQXQ");
   in_game[i][1].call ("re_show", null, arr);
   trace (in_game[i][1] + "callre_show");
  }
 }
 return;
};
//////////////////////////////////////////////////////////////////////////////////////////////////////


"rtmp://localhost/8.2"
main.as文件:



application.onAppStart = function() {
 //this.users_so = SharedObject.get('users_so');
 this.Lists = [];
};
application.onConnect = function(newClient, id, pass_num) {
 trace(id+"进来了");
 application.acceptConnection(newClient);
 newClient.id = id;
 this.Lists.push(id);
 //this.users_so.setProperty('users', this.Lists);
 application.broadcastMsg ("updata", this.Lists);
 trace(application.broadcastMsg ("updata", this.Lists))
};
application.onDisconnect = function(newClient) {
 for (var i = 0; i<this.Lists.length; i++) {
  if (this.Lists[i] == newClient.id) {
   this.Lists.splice(i, 1);
  }
 }
 trace(application.broadcastMsg ("updata", this.Lists))
 application.broadcastMsg ("updata", this.Lists);
 //this.users_so.setProperty('users', this.Lists);
};
Client.prototype.Msg=function(msg){
 trace(msg)
 _root.txt.text=msg+newline
 
 }




先发个初样~~庆祝下`~
视频教程列表
文章教程搜索
 
Flash AS推荐教程
Flash AS热门教程
看全部视频教程
购买方式/价格
购买视频教程: 咨询客服
tel:15972130058