flavien317
flavien317

Reputation: 48

One variable for two function in JS

I dev a little game, and I went at the beginning of the game, the gravity at -40, but after if the player wants to change it, he can.

I have this form:

<form>
     <input type="text" id="userInput" />
     <input type="submit" onclick="otherG();" />
</form>

My gravity is:

function initScene() {
   blablabla
   scene.setGravity(new THREE.Vector3(0, -40, 0));
   blablabla
}

How can I change the -40 by the input of the user, and allow the scene (no need do f5) ?

The form appear when the user wants to change gravity via a keypress.

PS: I'm using three.js and physijs.

Upvotes: 0

Views: 1661

Answers (1)

R3tep
R3tep

Reputation: 12854

Maybe something like this

HTML :

<form onsubmit="return otherG();">
   <input type="text" id="userInput" />
   <input type="submit" value="Change Gravity" />
</form>

Javascript :

var gravity = -40; // default value
var doc = document;

function otherG() {
    gravity = doc.getElementById("userInput").value;
    updateScene();
    return false;
}

function updateScene() {
  blablabla
  scene.setGravity(new THREE.Vector3(0, gravity, 0));
  blablabla
}

Upvotes: 2

Related Questions