actionscript 3 - (AS3) Two arrays associated? Change on one changes the other -


when splice entry 1 array, splices same entry other array... happening?

        private static var words:array = new wordfile().tostring().split(/\n/);         private static var wordstemp:array;  public static function checkword (word:string):boolean     { var truefalse:boolean = wordstemp.indexof(word+"\r".tolowercase()) > -1;                 trace (words.length)                  wordstemp.splice(wordstemp.indexof(word+"\r".tolowercase()), 1);                  trace (words.length)    return truefalse  } public static function resetarrays :void { wordstemp = words } 

with code, call resetarrays function every time new game started. once in game, program call checkword word being passed it. if found in word array, splice temporary array. however, when run it, 2 traces yield 2 different numbers, second 1 being 1 lower (assuming word found in array). seems me strange splicing temporary array gets reset, when tracing array supposed unchanged (there no operations other ones showing in it) seems changed splice in temporary array...

any thoughts?

in as3, data types except string , number (and related) copied by reference

this means copies reference original object when use myarray1 = myarray2

in more detail, consider memory @ words stored 0x123456

wordstemp = words make wordstemp point same memory, i.e. 0x123456

when operation on words, array @ 0x123456 modified. wordstemp refers to. in reality both same object. make both different, need clone object. in case of array, can clone using method modifies array , returns new array, e.g. array.slice

wordstemp=words.slice(0, words.length); //will trick 

or, concatenate nothing original array , duplicate

wordstemp=words.concat(); 

alternatively, if want write more lines of code, here's can do:

wordstemp=new array(); (var i:int=0; i<words.length; i++) {     wordstemp.push(words[i]); } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -