0

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.

6
  • Submit is refreshing the page. Add return false at the end of the function to prevent this. Commented Apr 3, 2014 at 15:15
  • the question appears to be about how to use user input in your code. make a variable and set it to the value that the user enters. use this variable in your code. Commented Apr 3, 2014 at 15:15
  • But not scene.setGravity(new THREE.Vector3(0, someNewValue, 0)); ? Commented Apr 3, 2014 at 15:16
  • Yes, but in intscene() i have many thing like draw car, the ground... Commented Apr 3, 2014 at 15:16
  • isn't scene a global var? Commented Apr 3, 2014 at 15:18

1 Answer 1

2

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
}
Sign up to request clarification or add additional context in comments.

3 Comments

in need change onclick="otherG"();" ?
ok, updateScene and initScene is same function or not ? because initScen draw all, but updatescene do same with new gravity ?
when i set the new gravity, the game freeze, i initscen with -40, if want chnage, the form call setnewgravity, and put 5, but frezze after that

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.