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 7733577
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T07:01:39+00:00 2026-06-01T07:01:39+00:00

I have a strange bug happening in my script. I have 3 balls on

  • 0

I have a strange bug happening in my script. I have 3 balls on the canvas, and I can click on anyone of them to select.

If I click another ball after one was selected, the new ball gets selected. Everytime a new ball is selected, the array of selected balls gets reset. So far so good.

What I would like to do is to reset the array of selected balls when I click on the empty space inside canvas.

But this only works for 1 of the balls (The bottom one, index # 2 in array).
But if I turn on alert to see what ball was clicked, all balls record the click.
And since I get the alert window pop up and the script freezes, I can see that the other balls are briefly selected aswell.

I have made this jsFiddle for you to look at. When you first load the fiddle the code that bugs is commented out, uncomment it see it.

The piece of code to look for is:

function selectBall(){
var mX = mouseX;
var mY = mouseY;
    for(i=0;i<balls.length;i++){
        if(balls[i].select(mX, mY)){
            ball.length = 0;
            ball.push(balls[i]);

            /* Uncomment this to see that click actually happens.
            alert('Ball ' + i + ' clicked');
            */

        }

        /* Uncomment this to get the bug.
        else {
            ball.length = 0;                    
        }
       */
    }
}

Why do I get this bug when the same line of code resets the array just fine when it’s inside the if statement but not when its under else?

How can I fix it?

  • 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-01T07:01:41+00:00Added an answer on June 1, 2026 at 7:01 am

    You are restting the ball list for every ball that is not clicked. This is incorrect. What you want to do is only reset the ball list if no balls were clicked. This can be fixed with the following.

    function selectBall(){
      var mX = mouseX;
      var mY = mouseY;
      var isClick = false;
      for(i=0;i<balls.length;i++){
        if(balls[i].select(mX, mY)){
            isClick = true;
            ball.length = 0;
            ball.push(balls[i]);
            break;
        }
      }
      if (!isClick)
      { 
        ball.length = 0;
      }
    }
    

    You can see that I added a simple flag to determine if any balls were selected or not. If none were, then I reset the array. Otherwise, I do not reset the array.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a rather strange bug which i can't make sense of that is
I have another strange bug which I cannot figure out. I try to create
I have a very strange bug in a shipping iPad/iPhone app that I can't
I have a strange bug. I launch a PHP Unit test Suite. At the
I have a very strange bug, which I believe is caused by some code
I have found what appears to be a strange bug with sqlite. I have
I have come across some strange behaviour, and I'm assuming a bug in Firefox,
I seem to have run into a strange bug or more likely some setting
I have a strange bug, please, let me know if you have any clues
I have a strange bug when trying to urlopen a certain page from Wikipedia.

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.