user2052251
user2052251

Reputation: 161

javascript code doesnt work

I am trying to use three.js library which is a javascript 3d library...

So the code snippet is this:

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);

So basically earlier this code .. the radius was hardcoded (second line) but instead of hardcoding radius.. I am reading it from json file which is following?

{
 "circles":

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

But it doesnt show anything? And I dont see any error in firebug as well Any suggestions? Thanks

Upvotes: 1

Views: 142

Answers (1)

Olaf Dietsche
Olaf Dietsche

Reputation: 74028

The call to $.getJSON(...) is asynchronous. So the order of statements is most likely

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

If you move scene.add(camera); into your success function, it should work as before.

Upvotes: 1

Related Questions