【问题标题】:Three.js object definition not definingThree.js 对象定义未定义
【发布时间】:2013-08-14 16:42:05
【问题描述】:

我正在学习一组教程,但遇到了一个我无法弄清楚的错误...

我知道这可能是我失明的原因,因为我在使用带有红色下划线的 IDE 进行的懒惰教育导致语法错误,但我有点需要尽快完成这项工作!

错误是 TypeError:俄罗斯方块.boundingBoxConfig 未定义 file:///C:/Users/Timmy/Documents/Emergent%20Tech/Tetris/js/tetris.js 127号线

我在这里定义俄罗斯方块.boundingBoxConfig

var boundingBoxConfig = {
    width: 360,
    height: 360,
    depth: 1200,
    splitX: 6,
    splitY: 6,
    splitZ: 20
  };

  Tetris.boundingBoxConfig = boundingBoxConfig;

这是第 127 行

mesh.position.x = (x - Tetris.boundingBoxConfig.splitX/2)*Tetris.blockSize + Tetris.blockSize/2;

如果您需要更多代码,请告诉我,我会进行编辑。任何帮助将非常感激!请只提出建设性的批评

编辑俄罗斯方块对象的定义

var Tetris = {};

还有第二种建议的方法是这样的:

window.Tetris = window.Tetris || {};

但我真的不明白这种方式是如何工作的

编辑 2

不确定这是否有助于澄清问题

var boundingBox = new THREE.Mesh(
  new THREE.CubeGeometry(
    boundingBoxConfig.width, boundingBoxConfig.height, boundingBoxConfig.depth, 
    boundingBoxConfig.splitX, boundingBoxConfig.splitY,         boundingBoxConfig.splitZ), 
  new THREE.MeshBasicMaterial( { color: 0xffaa00, wireframe: true } )
);
Tetris.scene.add(boundingBox);

【问题讨论】:

    标签: javascript three.js webgl


    【解决方案1】:

    在你的俄罗斯方块函数中,尝试改变

    Tetris.boundingBoxConfig = boundingBoxConfig;

    this.boundingBoxConfig = { //your definition };

    让您的财产公开。

    【讨论】:

    • 您好,感谢您的回答,但恐怕它不起作用
    【解决方案2】:

    原来这是因为我在 Firefox 中使用了本地文件。我知道 Chrome 在从本地磁盘加载文件时存在安全问题,因此使用 Firefox 认为由于其他项目正在运行而没有类似的问题。我错了。

    教训:永远不要使用本地文件总是使用服务器。

    【讨论】:

      【解决方案3】:

      能否请您包含定义“俄罗斯方块”对象的代码? (即 var Tetris blah blah blah)

      【讨论】:

      • 很难给出一个准确的答案,但希望这会让你走上正轨:使用“console.log(data);”在整个代码中输出 tetris 和 tetris.boundingBoxConfig 的值,以查看它在哪里设置为 null 或其他内容。定义后立即检查以确保定义已被采纳。这里还有一些关于 JS 中未定义错误的信息:javascriptweblog.wordpress.com/2010/08/16/…
      • console.log(Tetris.boundingBoxConfig);按预期输出每个变量,没有 NULL 值
      • 评论应该作为 cmets 发布,而不是答案。
      猜你喜欢
      • 2020-07-28
      • 2017-08-31
      • 2022-01-24
      • 2020-06-13
      • 2013-07-08
      • 2015-05-22
      • 2016-02-20
      • 2012-03-16
      • 2021-12-04
      相关资源
      最近更新 更多