0

I am writing a three.js application and need to access a variable called camera, because whenever I click a button I want to move the camera up and down.

However, my buttons are nested callbacks and I am wondering how can I accessing the variable within the callbacks?

e.g.

    let camera = ...
    let circles = document.querySelectorAll('circle');
    circles.forEach(function(c){
        c.onclick = function(e){
            circles.forEach(function(circle){
                circle.classList.remove('active');
                circle.classList.add('disabled');
            });
            e.target.classList.add('active');
            e.target.classList.remove('disabled');
            if (e.target.id === "example"){
                camera.position.set(0,0,0) //camera not defined
            }
            debugger;
        }
    });

I am a little confused why I can access the circles varaible at the debugger point, however it keeps saying camera is undefined.

J.Doe
  • 173
  • 1
  • 7
  • @iliar I set the initial position of the camera, I just tried to extreme simplify my example, e.g. camera.position.set( 0, 0, 10 ); – J.Doe Nov 16 '19 at 06:55
  • You cut out the portion of the code that is throwing the "undefined" error... ? And you want help... and there are people answering... hmmm – wahwahwah Nov 16 '19 at 06:56
  • @wahwahwah I am trying to access it inside the "...", e.g. camera.position.set(0,0,0) – J.Doe Nov 16 '19 at 07:00
  • What did you store into cemera variable..what does mean by ... In cemera veriable – Prabhat Nov 16 '19 at 07:30

1 Answers1

0

Ok, I am at a loss for words.

If I try to use the variable, e.g. camera.position.set(0,0,0) inside where I want to use it, I can print the variable inside the console.

If I do not use it, then I get an error.

The problem was me trying to debug and write tests first, before actually writing it.

J.Doe
  • 173
  • 1
  • 7