Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9253581
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:15:18+00:00 2026-06-18T11:15:18+00:00

My concept is rather simple. Animate an image one way, then make it come

  • 0

My concept is rather simple. Animate an image one way, then make it come back.

var xPos = 10;

function main(){
    window.requestAnimationFrame(main);
    var canvas = document.getElementById("myCanvas");
    var c = canvas.getContext("2d");

    //Initialize images
    var img = new Image();
    img.src = "goomba.png";

    c.clearRect(0,0,500,500);
    c.drawImage(img,xPos,10);


    //increment coordinates
    if(xPos <= 250){
        xPos+=2;
    }
    else if(xPos >= 250){
        xPos-=2;
    }
}
window.requestAnimationFrame(main);

I already know what the problem is with my code. When the xPos goes over 250, then the second if statement becomes true. However, once it goes below 250, the first if becomes true again.
So I know what the problem is, I just dont know how to fix it. Any help is appreciated!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T11:15:19+00:00Added an answer on June 18, 2026 at 11:15 am

    Have a velocity variable (positive is right, negative is left, amplitude is number of pixels to change each loop):

    var xVelocity = 2;
    

    And use it to determine what to add to the xPos each loop:

    xPos += xVelocity;
    

    Then set it based on xVelocity and xPos so that the image will “bounce”:

    if(xVelocity > 0){
        if(xPos > 250){
            xVelocity = -2;
        }
    } else {
        if(xPos < 10){
            xVelocity = 2;
        }    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My question is rather simple. I'm aware of the concept of a UUID and
Im making a simple proof of concept page with jQuery: $('.notclicked').click(function() { $(this).addClass(clicked); $(this).removeClass(notclicked);
newbie for clearcase. Since clearcase's config is rather different from other concept in git,
I am attempting to construct a very simple proof of concept that I can
Trying to implement what I thought was a simple concept. I have a user
I am doing something painfuly simple, and rather than looping over the list, and
I'm trying to understand a concept rather than fixing a piece of code that
I have rather simple, I imagine, question about CRTP , but I cannot seem
I'm designing a simple proof of concept for multitrack recorder. Obvious starting point is
I'm rather new to Python. Lately I was looking into the python's concept of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.