## Recommended Posts

I have a variable of holdSquare I set it to the object square. It updates after I change the values for square.x and square.y but it shouldn't change.

`function move(square){   var length = 16;    var holdSquare = square;     if (checkOverlap(square, number[length])){            alert(holdSquare.x);        square.x = number[length].x;        square.y = number[length].y;            alert(holdSquare.x);             number[length].x = holdSquare.x;       number[length].y = holdSquare.y;         }    }`

If you look at the first alert the value is correct at 250 for both x and y. At the second alert it changes to 325 for both x and y. number[length].x and .y are both 325. It seems as if holdSquare picks up this value and it shouldn't. My ultimate goal is to have square and number[length] trade values. Any thoughts as to what is going on?

##### Share on other sites

holdSquare and square are referencing the same object. Change one, you change the other, because they are the same thing.

You'll need to use temp X and Y variables separate to the square object.

`var x = square.x;var y = square.y;//or var temp = new Phaser.Point(square.x, square.y);`
##### Share on other sites

For the record, all objects (including arrays) are always referenced, whereas simple types (such as numbers and strings) are not. This leads to some unusual situations regarding equality, such as the following:

`// Below are comparisons of various objects using non-strict (==) equality// which you would expect to work in such situations as these. As you can see// even if objects have the exact same properties, they do not match equality.({x: 1, y: 1} == {x: 1, y: 1});false// Same goes for arrays, even empty ones[] == [];false// And built-in objectsnew Date() == new Date();false// However simple types (numbers, booleans, strings etc) are fine"abc" == "abc";true1 == 1;true`

If you want to copy a Phaser.Point object it has a handy clone method:

`var holdSquare = square.clone();`
{"ft_lang_label":"__label__en","ft_lang_prob":0.87950045,"math_prob":0.99567026,"size":900,"snap":"2023-14-2023-23","text_gpt3_token_len":226,"char_repetition_ratio":0.19642857,"word_repetition_ratio":0.014184397,"special_character_ratio":0.26555556,"punctuation_ratio":0.18090452,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.99355584,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-03-31T09:12:58Z\",\"WARC-Record-ID\":\"<urn:uuid:5abf0e53-a069-4311-b77b-04a50ede2e92>\",\"Content-Length\":\"106175\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:218b7263-b556-4dda-a48e-d465cc6ed308>\",\"WARC-Concurrent-To\":\"<urn:uuid:88ee206b-c11c-46a8-b326-ba83914b0243>\",\"WARC-IP-Address\":\"104.26.7.191\",\"WARC-Target-URI\":\"https://www.html5gamedevs.com/topic/6814-variable-updating-when-it-shouldnt/\",\"WARC-Payload-Digest\":\"sha1:NNC5JW6SUBH5FUEG74OJOZO7ZGBKQLPR\",\"WARC-Block-Digest\":\"sha1:VFFCRRULJSNBNYA4PQNQAEBLZOUYKAX4\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-14/CC-MAIN-2023-14_segments_1679296949598.87_warc_CC-MAIN-20230331082653-20230331112653-00018.warc.gz\"}"}