Create an application with one DOM element that is always moving right, at a speed of 2 pixels per update
Create an application with one DOM element that is always moving right, at a speed of 2 pixels per update. When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').
This is what I have so far but I can't get the ball to bounce to the left.
var ball = document.querySelector(".ball");
var animationArea = document.querySelector("#animationArea");
setInterval(update, 30); //this will call the tick function every 1000 miliseconds
var x = 0;
var y = 0;
var velocityX=2;
var velocityY=2;
function update()
//run your update code
x = x + velocityX;
y = y + velocityY;
ball.style.right = x + "px";
ball.style.left = y + "px";
if (x>=800)
//bounce the ball
velocityX = 2;
y = 300;
requestAnimationFrame(update);
javascript
add a comment |
Create an application with one DOM element that is always moving right, at a speed of 2 pixels per update. When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').
This is what I have so far but I can't get the ball to bounce to the left.
var ball = document.querySelector(".ball");
var animationArea = document.querySelector("#animationArea");
setInterval(update, 30); //this will call the tick function every 1000 miliseconds
var x = 0;
var y = 0;
var velocityX=2;
var velocityY=2;
function update()
//run your update code
x = x + velocityX;
y = y + velocityY;
ball.style.right = x + "px";
ball.style.left = y + "px";
if (x>=800)
//bounce the ball
velocityX = 2;
y = 300;
requestAnimationFrame(update);
javascript
Please fix the mismatch between the comment and the setInterval line in the code sample, it is a major OCD trigger :-)
– Paul-Jan
Nov 14 '18 at 16:23
you probably don't want to be calling the update function with bothsetInterval
andrequestAnimationFrame
...
– Punit
Nov 14 '18 at 17:40
also, the ball's x coordinate should correspond with itsleft
property, and the y coordinate should correspond with itstop
property
– Punit
Nov 14 '18 at 17:42
add a comment |
Create an application with one DOM element that is always moving right, at a speed of 2 pixels per update. When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').
This is what I have so far but I can't get the ball to bounce to the left.
var ball = document.querySelector(".ball");
var animationArea = document.querySelector("#animationArea");
setInterval(update, 30); //this will call the tick function every 1000 miliseconds
var x = 0;
var y = 0;
var velocityX=2;
var velocityY=2;
function update()
//run your update code
x = x + velocityX;
y = y + velocityY;
ball.style.right = x + "px";
ball.style.left = y + "px";
if (x>=800)
//bounce the ball
velocityX = 2;
y = 300;
requestAnimationFrame(update);
javascript
Create an application with one DOM element that is always moving right, at a speed of 2 pixels per update. When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').
This is what I have so far but I can't get the ball to bounce to the left.
var ball = document.querySelector(".ball");
var animationArea = document.querySelector("#animationArea");
setInterval(update, 30); //this will call the tick function every 1000 miliseconds
var x = 0;
var y = 0;
var velocityX=2;
var velocityY=2;
function update()
//run your update code
x = x + velocityX;
y = y + velocityY;
ball.style.right = x + "px";
ball.style.left = y + "px";
if (x>=800)
//bounce the ball
velocityX = 2;
y = 300;
requestAnimationFrame(update);
javascript
javascript
edited Nov 14 '18 at 17:21
Alfredo A.
98011633
98011633
asked Nov 14 '18 at 16:20
seanstacksseanstacks
61
61
Please fix the mismatch between the comment and the setInterval line in the code sample, it is a major OCD trigger :-)
– Paul-Jan
Nov 14 '18 at 16:23
you probably don't want to be calling the update function with bothsetInterval
andrequestAnimationFrame
...
– Punit
Nov 14 '18 at 17:40
also, the ball's x coordinate should correspond with itsleft
property, and the y coordinate should correspond with itstop
property
– Punit
Nov 14 '18 at 17:42
add a comment |
Please fix the mismatch between the comment and the setInterval line in the code sample, it is a major OCD trigger :-)
– Paul-Jan
Nov 14 '18 at 16:23
you probably don't want to be calling the update function with bothsetInterval
andrequestAnimationFrame
...
– Punit
Nov 14 '18 at 17:40
also, the ball's x coordinate should correspond with itsleft
property, and the y coordinate should correspond with itstop
property
– Punit
Nov 14 '18 at 17:42
Please fix the mismatch between the comment and the setInterval line in the code sample, it is a major OCD trigger :-)
– Paul-Jan
Nov 14 '18 at 16:23
Please fix the mismatch between the comment and the setInterval line in the code sample, it is a major OCD trigger :-)
– Paul-Jan
Nov 14 '18 at 16:23
you probably don't want to be calling the update function with both
setInterval
and requestAnimationFrame
...– Punit
Nov 14 '18 at 17:40
you probably don't want to be calling the update function with both
setInterval
and requestAnimationFrame
...– Punit
Nov 14 '18 at 17:40
also, the ball's x coordinate should correspond with its
left
property, and the y coordinate should correspond with its top
property– Punit
Nov 14 '18 at 17:42
also, the ball's x coordinate should correspond with its
left
property, and the y coordinate should correspond with its top
property– Punit
Nov 14 '18 at 17:42
add a comment |
1 Answer
1
active
oldest
votes
The solution is in the question itself:
When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').
if (x >= 800) // When it it reaches 800 pixels to the right (a 'left' property of '800px')
x = 0; // return it to the far left of the screen (a 'left' property of '0px')
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304604%2fcreate-an-application-with-one-dom-element-that-is-always-moving-right-at-a-spe%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
The solution is in the question itself:
When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').
if (x >= 800) // When it it reaches 800 pixels to the right (a 'left' property of '800px')
x = 0; // return it to the far left of the screen (a 'left' property of '0px')
add a comment |
The solution is in the question itself:
When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').
if (x >= 800) // When it it reaches 800 pixels to the right (a 'left' property of '800px')
x = 0; // return it to the far left of the screen (a 'left' property of '0px')
add a comment |
The solution is in the question itself:
When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').
if (x >= 800) // When it it reaches 800 pixels to the right (a 'left' property of '800px')
x = 0; // return it to the far left of the screen (a 'left' property of '0px')
The solution is in the question itself:
When it it reaches 800 pixels to the right (a 'left' property of '800px'), return it to the far left of the screen (a 'left' property of '0px').
if (x >= 800) // When it it reaches 800 pixels to the right (a 'left' property of '800px')
x = 0; // return it to the far left of the screen (a 'left' property of '0px')
answered Nov 14 '18 at 17:49
PunitPunit
96749
96749
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53304604%2fcreate-an-application-with-one-dom-element-that-is-always-moving-right-at-a-spe%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Please fix the mismatch between the comment and the setInterval line in the code sample, it is a major OCD trigger :-)
– Paul-Jan
Nov 14 '18 at 16:23
you probably don't want to be calling the update function with both
setInterval
andrequestAnimationFrame
...– Punit
Nov 14 '18 at 17:40
also, the ball's x coordinate should correspond with its
left
property, and the y coordinate should correspond with itstop
property– Punit
Nov 14 '18 at 17:42