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