/    Sign up×
Community /Pin to ProfileBookmark

How do you use html input to change a global variable

have been learning javascript for a few days and have been practising on a bouncing ball game. Anyway, I am trying to get html user input to change the speed of the ball, but it does not seem to work. I created a doStuff function to grab the html userinput value and tried to set this to the ball speed, but it does not change the initial speed that I gave it. Can someone show me why html input does not change the value bsp variable and what I should add to change it. The code is below

[code]// declare variables
const FPS = 60;
var bs = 30;
var bx, by;
var xv, yv;
var canvas, context;
var bsp = 400;

// load canvas
canvas = document.getElementById(“gameCanvas”);
context = canvas.getContext(“2d”);

// set up interval (game loop)
setInterval(update, 1000 / FPS);

function doStuff() {
var nameElement = document.getElementById(“someInput”);

// ball speed
bsp = nameElement.value;
}

doStuff();
// ball starting position
bx = canvas.width / 2;
by = canvas.height / 2;

// random ball starting speed (between 10 and 60 pps)
xv = Math.floor(Math.random() * 10 + bsp) / FPS;
yv = Math.floor(Math.random() * 10 + bsp) / FPS;

// random ball direction
if (Math.floor(Math.random() * 2) == 0) {
xv = -xv;
}
if (Math.floor(Math.random() * 2) == 0) {
yv = -yv;
}

// update function
function update() {
// move the ball
bx += xv;
by += yv;

// bounce the ball off each wall
if (bx – bs / 2 < 0 && xv < 0) {
xv = -xv;
}
if (bx + bs / 2 > canvas.width && xv > 0) {
xv = -xv;
}
if (by – bs / 2 < 0 && yv < 0) {
yv = -yv;
}
if (by + bs / 2 > canvas.height && yv > 0) {
yv = -yv;
}

// draw background and ball
context.fillStyle = “white”;
context.fillRect(0, 0, canvas.width, canvas.height);

context.beginPath();
context.fillStyle = “#CCCC00”;
context.arc(bx, by, bs, 0, Math.PI * 2, true);
context.fillStyle = “#000000”;
context.closePath();
context.fill();
}[/code]

[code]<!DOCTYPE html>

<html>

<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>ball</title>

<body>

<head>
<title>Ball Speed</title>
</head>
<h1>Ball Speed</h1>

<body>

<br>
<input id=”someInput” type=”number”>
<input type=”button” value=”Sumbit” onClick=”doStuff()”>
</body>

<canvas id=”gameCanvas” width=”1900″ height=”750″></canvas>

</html>[/code]

to post a comment
JavaScript

3 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJun 03.2020 — Your update function does not use the speed `bsp</C> directly but the variables <C>xv</C> and <C>yv</C>. The values of these are not calculated again when the speed is changed. You need to place that code inside the function <C>doStuff</C>.<br/>
This code works for me:
<CODE>&amp;lt;!DOCTYPE html&amp;gt;

&amp;lt;html&amp;gt;

&amp;lt;head&amp;gt;
&amp;lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=utf-8&quot; /&amp;gt;
&amp;lt;title&amp;gt;ball&amp;lt;/title&amp;gt;

&amp;lt;/head&amp;gt;

&amp;lt;body&amp;gt;
&amp;lt;h1&amp;gt;Ball Speed&amp;lt;/h1&amp;gt;

&lt;i&gt; &lt;/i&gt;&amp;lt;br&amp;gt;
&lt;i&gt; &lt;/i&gt;&amp;lt;input id=&quot;someInput&quot; type=&quot;number&quot; value=&quot;100&quot;&amp;gt;
&lt;i&gt; &lt;/i&gt;&amp;lt;input type=&quot;button&quot; value=&quot;Submit&quot; onclick=&quot;setBallParams()&quot;&amp;gt;
&lt;i&gt; &lt;/i&gt;&amp;lt;canvas id=&quot;gameCanvas&quot; width=&quot;600&quot; height=&quot;400&quot;&amp;gt;&amp;lt;/canvas&amp;gt;

&lt;i&gt; &lt;/i&gt;&amp;lt;script&amp;gt;
&lt;i&gt; &lt;/i&gt; // declare variables
&lt;i&gt; &lt;/i&gt; const FPS = 60;
&lt;i&gt; &lt;/i&gt; var bs = 30;
&lt;i&gt; &lt;/i&gt; var bx, by;
&lt;i&gt; &lt;/i&gt; var xv, yv;
&lt;i&gt; &lt;/i&gt; var canvas, context;
&lt;i&gt; &lt;/i&gt; var bsp = 400;

&lt;i&gt; &lt;/i&gt; // load canvas
&lt;i&gt; &lt;/i&gt; canvas = document.getElementById(&quot;gameCanvas&quot;);
&lt;i&gt; &lt;/i&gt; context = canvas.getContext(&quot;2d&quot;);

&lt;i&gt; &lt;/i&gt; // set up interval (game loop)
&lt;i&gt; &lt;/i&gt; setInterval(update, 1000 / FPS);

&lt;i&gt; &lt;/i&gt; function setBallParams() {
&lt;i&gt; &lt;/i&gt; var nameElement = document.getElementById(&quot;someInput&quot;);

&lt;i&gt; &lt;/i&gt; // ball speed
&lt;i&gt; &lt;/i&gt; bsp = parseInt(nameElement.value);

&lt;i&gt; &lt;/i&gt; // random ball starting speed (between 10 and 60 pps)
&lt;i&gt; &lt;/i&gt; xv = Math.floor(Math.random() * 10 + bsp) / FPS;
&lt;i&gt; &lt;/i&gt; yv = Math.floor(Math.random() * 10 + bsp) / FPS;

&lt;i&gt; &lt;/i&gt; // random ball direction
&lt;i&gt; &lt;/i&gt; if (Math.floor(Math.random() * 2) == 0) {
&lt;i&gt; &lt;/i&gt; xv = -xv;
&lt;i&gt; &lt;/i&gt; }
&lt;i&gt; &lt;/i&gt; if (Math.floor(Math.random() * 2) == 0) {
&lt;i&gt; &lt;/i&gt; yv = -yv;
&lt;i&gt; &lt;/i&gt; }
&lt;i&gt; &lt;/i&gt; }

&lt;i&gt; &lt;/i&gt; setBallParams();
&lt;i&gt; &lt;/i&gt; // ball starting position
&lt;i&gt; &lt;/i&gt; bx = canvas.width / 2;
&lt;i&gt; &lt;/i&gt; by = canvas.height / 2;

&lt;i&gt; &lt;/i&gt; // update function
&lt;i&gt; &lt;/i&gt; function update() {
&lt;i&gt; &lt;/i&gt; // move the ball
&lt;i&gt; &lt;/i&gt; bx += xv;
&lt;i&gt; &lt;/i&gt; by += yv;

&lt;i&gt; &lt;/i&gt; // bounce the ball off each wall
&lt;i&gt; &lt;/i&gt; if (bx - bs / 2 &amp;lt; 0 &amp;amp;&amp;amp; xv &amp;lt; 0) { xv = -xv; } if (bx + bs / 2 &amp;gt; canvas.width &amp;amp;&amp;amp; xv &amp;gt; 0) {
&lt;i&gt; &lt;/i&gt; xv = -xv;
&lt;i&gt; &lt;/i&gt; }
&lt;i&gt; &lt;/i&gt; if (by - bs / 2 &amp;lt; 0 &amp;amp;&amp;amp; yv &amp;lt; 0) { yv = -yv; } if (by + bs / 2 &amp;gt; canvas.height &amp;amp;&amp;amp; yv &amp;gt; 0) {
&lt;i&gt; &lt;/i&gt; yv = -yv;
&lt;i&gt; &lt;/i&gt; }

&lt;i&gt; &lt;/i&gt; // draw background and ball
&lt;i&gt; &lt;/i&gt; context.fillStyle = &quot;white&quot;;
&lt;i&gt; &lt;/i&gt; context.fillRect(0, 0, canvas.width, canvas.height);

&lt;i&gt; &lt;/i&gt; context.beginPath();
&lt;i&gt; &lt;/i&gt; context.fillStyle = &quot;#CCCC00&quot;;
&lt;i&gt; &lt;/i&gt; context.arc(bx, by, bs, 0, Math.PI * 2, true);
&lt;i&gt; &lt;/i&gt; context.fillStyle = &quot;#000000&quot;;
&lt;i&gt; &lt;/i&gt; context.closePath();
&lt;i&gt; &lt;/i&gt; context.fill();
&lt;i&gt; &lt;/i&gt; }
&lt;i&gt; &lt;/i&gt;&amp;lt;/script&amp;gt;
&amp;lt;/body&amp;gt;

&amp;lt;/html&amp;gt;
</CODE>

Some additional remarks:

Your HTML was faulty: head section twice, h1 and canvas outside of body.

I recommend using meaningful names for variables and functions. E. g. I replaced <C>
doStuff</C> by <C>setBallParams</C>.

I recommend using requestAnimationFrame as MDN does:<br/>
<URL url="https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame">https://developer.mozilla.org/en-US/docs/Web/API/window/requestAnimationFrame</URL>

Single backticks don't work reliably, please use code tags when posting code: <C>
your code here`


I edited your posting accordingly.
Copy linkTweet thisAlerts:
@shanaboxerauthorJun 03.2020 — Thank you very much. I am just starting to learn javascript so your advice is most helpful.
Copy linkTweet thisAlerts:
@VITSUSAJun 04.2020 — @Sempervivum#1619096 This is really good solution :)
×

Success!

Help @shanaboxer spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 4.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...