I can't call this function

I’m making a game in which I would like all of the world to be inside one variable. But whenever I tried to do it, this problem came up in the JavaScript console:

Uncaught TypeError: Cannot read properties of
undefined (reading 'createGround') 
    at code.html:36:22

Here’s the link to my code:

https://www.code3dgames.com/3de/#B/tVhbU9s4FH7vr9DwEmc3OCGQbpeWzrCU0s5SYEja7ltHsZVYxba8kpzU7fDf9xxJvibpULplCMRH5/KdqyW9mIuwePliaP49eaECyTNNlAxO9oY6koz5n9UerNuFLkcWFYp/lyMQqZYiVsNrOef6zD09TOR1XHxf4OUTQoZDcoMoAkUU05qnSwVUQ/qsfMunfJokgpyQ3hC/gLLe8y1MayHvmDRsmV36ZElWwBqbRYzsqYClbI9wRdYRk4wonS8WhKdE5JIsacLImscxiWiWsfQYBFdUEiME2lO2rkxPkeb1EY1Z9sGHC0lXXBce8s3e3J6f+x9YoIU89MhoQPYPRvB3RPpGCPUuYqpB7Tf8P41oCCE4Jlrm7P65gYw8MV9G2tm2Ok+TOWepvsQFr7eOuGY90Os/61spC4eGoWdk+y3/A3BRUhcAsK4YU0RHLhClw1RlAPyWao6xX/M0FGufpymTH3moIzJs0d4wtFM6ZS20EN8waRTyFTszq94fk0HTyIAcwO8IfowPVoWfCcVhMfW/grbxZFTHGp2zTG3vJEtDyKrcI6Gka+cieudAGW+hMIV1OYAuqZJcyraQf2Tzi8tbt+J9o6nmNOZUuTQZtKUgVsCUf2XeRrwGW8JlREMR5Ank0scm9k3NhWcRj0OvUhqK5DxmyGQkjLM1vlZvupAMyHZhG6ff3A+Zzk5vZ+Ts+tXbqwtyfQUKz8nV+T8zcvn26rxisyZNXnMJevWZLJSmMfZaweJYrHtl4rF7rlcmgAsaK1bSY4gakymQIW2X9sGremApRZ6GWGVCxqEPKaGaXRiiZQKhv2gce/26J9bwrKy+j/i1XINnB8+QgOZstwtms8CKusDcUizE3ane0snYyO+ojvybt9AGY2g8288mTtYl4wn2taGAyoZPx2SRpwEa9foVg5syMHRYq/rO8jm7YCJhWhaeaQ+wPh65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teKqEQIHVm1TM+AXeTaK03aI+fw6agCXYNx6XjSuM6AuXa+MndUSgNE6fXuWBHioWUA2tPwb1a8gqdStD6t1WseQwXNjAUixNo3dB8fnlcdY5dOSO9UwtHofdbr16NkFxcaAb6683cxXsIUBkY7jHcx3eLIBC43OrewTTMaMOCw7e4ar7oY/Q8=

Thanks! :grin::grin::grin:

Moldy-Mango

This is another case of JavaScript being fussy about when things are defined. In this case, you need to move the world definition above when you call world.createGround():

  var currentCrystal = 'yellow';
  var gameOver = false;
  var lantern = addLantern();
  
  var world = {
    createGround: function() {
      var shape = new THREE.CubeGeometry(100000, 20, 100000);
      var cover = new THREE.MeshPhongMaterial({color: 'white'});
      var plane = new Physijs.BoxMesh(shape, cover, 0);
      scene.add(plane);
      plane.__dirtyPosition = true;
      plane.position.y = -16.5;
    },
  };
  
  var ground = world.createGround();
  addBall();

Aside from that, everything looks good!

-Chris

Thanks for the help! :grin: