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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:51:29+00:00 2026-06-10T14:51:29+00:00

function Block(){ this.x = 100; this.y = 100; } // update canvas function UpdateCanvas(){

  • 0
function Block(){
    this.x = 100;
    this.y = 100;
}

// update canvas
        function UpdateCanvas(){
            // check if game is still on going
            if (!isStopped)
            {
                for(var i = segments.length - 1; i > 0 ; i--)
                {
                    segments[i] = segments[i - 1];
                }

                switch(direction)
                {
                    case "left":
                        segments[0].x = segments[0].x - pixelChange;
                        DrawCanvas();
                        break;
                    case "right":
                        segments[0].x = segments[0].x + pixelChange;
                        DrawCanvas();
                        break;
                    case "up":
                        segments[0].y = segments[0].y - pixelChange;
                        DrawCanvas();
                        break;
                    case "down":
                        segments[0].y = segments[0].y + pixelChange;
                        DrawCanvas();
                        break;
                }
            }
        }

I made an array and a class called Block. Now after creating 2 objects in the array and changing blockArray[0]’s x property to a different number, blockArray[1]’s x is also changed similar to blockArray[0]’s x value instead of retaining the initial value of 100.

What was I doing wrong? and how do I change blockArray[0]’s x without affecting other objects in the array?

EDIT:
updated the code. segments is an Array with Block elements.

  • 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-10T14:51:31+00:00Added an answer on June 10, 2026 at 2:51 pm

    fixed thanks. Norguard’s and Mark Reed’s comments led me to finding the wrong part of the code (which I forgot to include on my post).

    doing:

    for(var i = segments.length - 1; i > 0 ; i--){
        segments[i] = segments[i - 1];
    }
    

    saves a reference of segments[i – 1] to segments[i] instead of a saving an object. Lesson learned for me.

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

Sidebar

Related Questions

$(function() { $(table.section thead).click(function() { if ($(this).next(table.section tbody).style.display == block){ $(this).next(table.section tbody).slideUp(slow); } if
So I have this function: (define (try try-block catch-block finally-block) ; Implements try/catch/finally like
This is clearly not the case though. My JS : $(.job_charge.item-block).live({ mouseenter: function(){ $(this).find('.edit-and-delete').stop(true,true).fadeIn();
I have a JS code like this <script type=text/javascript> $(document).ready(function(){ $('.main').on('click', '.block', function (){
here is my code: $(a).live('click', function(){ history.pushState({}, '', this.href); popstate(this.href); $(#loadpage).css(display, block); return false;
I'm building a game in Javascript using canvas which requires collision detection, in this
I have this code: $(document).ready(function(){ $(.links a).click(function(e){ var toLoad = products.html # + this.id;
The jQuery: $(document).ready( function() { $(#links .button).click(function() { var id = $(this).attr(id) + -fade;
I have got this html: <a style=display:block; padding:100%; text-decoration: none; href=http://google.com class=Jasmin id=target_site_to_visit> <span
This code works... $(document).ready(function(){ $('body').css('background','black'); }); but this code doesn't... $(document).ready(function(){ $('thumb_slider').css('background','black'); }); ^^^the

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.