2013-02-23 81 views
1

我想使用three.js所库,它是一个JavaScript 3D库...javascript代码不工作

因此,代码片段是这样的:

var segments = 16, rings = 16; 
//var radius = 50; <-- original 

// create a new mesh with sphere geometry - 
// we will cover the sphereMaterial next! 
var scene = new THREE.Scene(); 
// My read from file snippet. 
$.getJSON('sample.json', function(data) { 
    var radius = data.circles.r; 
    console.log(radius); 


    var sphere = new THREE.Mesh(
     new THREE.SphereGeometry(radius, segments, rings), 
     sphereMaterial); 

    // add the sphere to the scene 
    scene.add(sphere); 
}); 
// and the camera 
scene.add(camera); 

所以基本上早些时候代码..半径硬编码(第二行),而不是硬编码半径..我正在从json文件中读取它正在跟踪?

{ 
"circles": 

{"x":14,"y":2,"z":4,"r":20} 
} 

但它不显示任何东西?而且我在萤火虫中也看不到任何错误 有什么建议吗? 谢谢

+0

它是否直接为您工作,没有JSON调用? – 2013-02-23 22:14:14

回答

1

$.getJSON(...)的调用是异步的。所以声明的顺序很可能是

  1. var scene = new THREE.Scene();
  2. scene.add(camera);
  3. var sphere = new THREE.Mesh(...);
  4. scene.add(sphere);

如果您将scene.add(camera);移动到您的成功函数中,它应该像以前一样工作。

+0

谢谢..它不工作。我收到一条警告:“DEPRECATED:摄像头没有被添加到场景中,将它添加到...” 什么是正确的方式让这个东西读取数据并将其作为参数传递? – user2052251 2013-02-23 21:34:22

+0

@ user2052251更改为'getJSON'之前的顺序是什么? – 2013-02-23 21:35:36

+0

http://jsfiddle.net/26PZg/ – user2052251 2013-02-23 21:45:55