鱼C论坛

 找回密码
 立即注册
查看: 2856|回复: 5

[技术交流] js五子棋, 游戏主循环使用window.setInterval,大概在十几秒之后电脑卡死

[复制链接]
发表于 2020-2-19 21:29:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
代码中没有涉及到过多的变量读写,程序执行过程中变量的值正常,任务管理器cpu,内存占用正常,但是电脑就是会变卡, 检查了多遍就是不解, 求解决!!!!


  1. // 全局属性
  2. var c = null,
  3.     ctx = null,
  4.     // 地图
  5.     map = null,
  6.     // 黑棋、白棋,黑棋预落子,白棋预落子
  7.     blackPiece = null,
  8.     whitePiece = null,
  9.     blackPieceA = null,
  10.     whitePieceA = null,
  11.     // 格子,棋子的大小
  12.     size = null,
  13.     // size的一半
  14.     halfSize = null,
  15.     // 画线的数量
  16.     number = null,
  17.     // 鼠标所在的X,Y坐标
  18.     // mapX = null,
  19.     // mapY = null,
  20.     // 频繁读取的坐标变量
  21.     x = null,
  22.     y = null,
  23.     // 游戏帧率
  24.     fps = null;

  25. window.onload = function () {

  26.     c = document.getElementById("myCanvas");
  27.     ctx = c.getContext("2d");
  28.     ctx.strokeStyle = "#444041";
  29.     fps = 1000 / 10;
  30.     size = 50;
  31.     halfSize = size / 2;
  32.     number = 16;
  33.     // mapX = halfSize;
  34.     // mapY = halfSize;

  35.     blackPiece = document.createElement('img');
  36.     blackPiece.src = "./imgs/black.png";

  37.     whitePiece = document.createElement('img');
  38.     whitePiece.src = "./imgs/white.png";

  39.     blackPieceA = document.createElement('img');
  40.     blackPieceA.src = "./imgs/_black.png";

  41.     whitePieceA = document.createElement('img');
  42.     whitePieceA.src = "./imgs/_white.png";
  43.     // 地图的初始化, 1-黑棋, 2-白棋, 0-无子
  44.     initMap();
  45.     console.log(map);
  46.     // 游戏开始
  47.     begin();
  48. };

  49. function initMap() {
  50.     map = [];
  51.     for (let x = 0; x < number; x++) {
  52.         map[x] = [];
  53.         for (let y = 0; y < number; y++) {
  54.             map[x][y] = 0;
  55.         }
  56.     }
  57. }

  58. function drawCheckerboard() {
  59.     // 准备重绘棋盘
  60.     ctx.clearRect(0, 0, c.width, c.height);
  61.     // 画棋盘线条
  62.     for(let x = 0; x < number; x++) {
  63.         ctx.moveTo((x + 1) * size, size);
  64.         ctx.lineTo((x + 1) * size, number * size);
  65.     }
  66.     for(let y = 0; y < number; y++) {
  67.         ctx.moveTo(size, (y + 1) * size);
  68.         ctx.lineTo(number * size, (y + 1) * size);
  69.     }
  70.     ctx.stroke();
  71.     // 绘制棋盘上的棋子
  72.     for (let x = 0; x < number; x++) {
  73.         for (let y = 0; y < number; y++) {
  74.             if(1 == map[x][y]) {
  75.                 ctx.drawImage(blackPiece, (x + 0.5) * size, (y + 0.5) * size, size, size);
  76.             }
  77.             else if(2 == map[x][y]) {
  78.                 ctx.drawImage(whitePiece, (x + 0.5) * size, (y + 0.5) * size, size, size);
  79.             }
  80.         }
  81.     }
  82. }

  83. function drawPos() {
  84.     // 判断鼠标是否在棋盘
  85.     if(x < halfSize || y < halfSize ||
  86.         x > c.width - halfSize || y > c.height - halfSize) {
  87.         return;
  88.     }
  89.     console.log("mapX: ", mapX, " mapY:", mapY)
  90.     console.log("   x: ", x, "   y: ", y);


  91.     ctx.drawImage(whitePieceA, x, y, size, size);
  92. }

  93. function begin() {
  94.     window.setInterval(
  95.         // 获取当前坐标
  96.         function () {
  97.             c.onmousemove = function (e) {
  98.                 // 如果此时落子,则落在(x,y)处, 圆心为(x+halfSize, y+halfSize)
  99.                 x = Math.floor((e.layerX + halfSize) / size) * size - halfSize;
  100.                 y = Math.floor((e.layerY + halfSize) / size) * size - halfSize;
  101.                 return;
  102.             }
  103.             drawCheckerboard();
  104.             drawPos();
  105.         },
  106.         fps
  107.     );
  108. }
复制代码




                               
登录/注册后可看大图

上面是代码, 下面有压缩包,感谢解答!!!
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2020-2-20 00:53:06 | 显示全部楼层
木有发现压缩包。。。
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-2-20 08:14:10 | 显示全部楼层
下载包没看到,没进行代码测试。

按照你说的,往往是“内存泄露”,循环没有“终止”,CPU 跑满,浏览器自动杀掉“进程”
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-20 16:11:02 | 显示全部楼层
sukiwhip 发表于 2020-2-20 00:53
木有发现压缩包。。。

掉了, 尴尬
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2020-2-20 16:12:12 | 显示全部楼层
不二如是 发表于 2020-2-20 08:14
下载包没看到,没进行代码测试。

按照你说的,往往是“内存泄露”,循环没有“终止”,CPU 跑满,浏览器 ...

任务管理器里面内存和cpu都在20一下, 但就是很卡
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2020-5-16 13:13:50 | 显示全部楼层
其实,很多编程语言都有这个内存泄漏的问题。你可以判断,如果这个Interval让CPU耗到一定高度,马上刷新页面 ,重新启动Interval
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-18 05:52

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表