That shouldn't happen if you use, e.g., 'board = source.board.concat();' as opposed to 'board = source.board;'. Using concat() without any arguments creates a new Array object with the same data as the original, but distinct from it:
var original:Array = [1, 2, 3]; var identical:Array = original; var copy:Array = original.concat(); original.push(4); trace(original); // Outputs "1,2,3,4". trace(identical); // Outputs "1,2,3,4". trace(copy); // Outputs "1,2,3". ― Mike Keesey > -----Original Message----- > From: [EMAIL PROTECTED] [mailto:flashcoders- > [EMAIL PROTECTED] On Behalf Of Paul Steven > Sent: Friday, November 03, 2006 9:55 AM > To: 'Flashcoders mailing list' > Subject: RE: [Flashcoders] Copy Constructor > > Seem to be having problems with this still. > > It seems like every time I change a value in the main constructor objects > array, it also changes the array values in the copies. > > Are these copies essentially just pointers to the same array? > > Basically the reason I need copies is so it can simulate future moves in > the > game by updating a copy of the game state arrays. > > So essentially I need to create copies of these arrays that I can > manipulate > without these manipulations affecting the arrays they were copied from. > > I hope that makes sense > > Thanks > > Paul > > -----Original Message----- > From: [EMAIL PROTECTED] > [mailto:[EMAIL PROTECTED] On Behalf Of Mike > Keesey > Sent: 02 November 2006 22:53 > To: 'Flashcoders mailing list' > Subject: RE: [Flashcoders] Copy Constructor > > I might do it like this: > > class mypackage.Connect4State extends Object { > public function Connect4State(state:Connect4State) { > super(); > if (state instanceof Connect4State) { > copy(state); > } else { > board = new Array(); > score = new Array(); > // Default initialization code. > } > } > public function copy(state:Connect4State):Void { > board = state.board.concat(); > score = state.score.concat(); > } > public var board:Array; > public var score:Array; > } > > Then you could call the construct with or without a parameter. > > (Actually, I would never use public variables, but that's another > topic....) > ― > Mike Keesey > > > -----Original Message----- > > From: [EMAIL PROTECTED] [mailto:flashcoders- > > [EMAIL PROTECTED] On Behalf Of Paul Steven > > Sent: Thursday, November 02, 2006 2:31 PM > > To: 'Flashcoders mailing list' > > Subject: RE: [Flashcoders] Copy Constructor > > > > Perhaps it is easier if I explain what I need: > > > > I am rewriting some Java code into AS2. > > > > Basically I have a class called Connect4State > > > > class Connect4State { > > > > public function Connect4State() { > > > > // ------------------------- > > // Initialize the board array > > // -------------------------- > > > > board = new Array(); > > > > code goes here > > > > > > // -------------------------- > > // Initialize the score array > > // -------------------------- > > > > score = new Array(); > > > > code goes here > > } > > > > public var board:Array; // A 7 by 6 two dimensional array of > > integers representing the state of game > > > > public var score:Array; // A 2 dimensional array of integers > > representing the "score" for the players > > > > } > > > > The Java code has a function called a "Copy Constructor" that enables > you > > to > > create new objects that are copies of existing objects. The copy > > constructor > > for Connect4State just copies the contents of each member variable. It > is > > necessary to have a copy constructor for Connect4State because the AI > > algorithms use temporary state objects a great deal > > > > This is the "copy constructor" code translated to AS2 > > > > public function Connect4State(state:Connect4State) { > > > > // -------------- > > // Copy the board > > // -------------- > > > > board = new Array(); > > > > for (var i:Number = 0; i < 7; i++) { > > > > board[i] = new Array(); > > > > for (j:Number = 0; j < 6; j++) { > > > > board[i][j] = state.board[i][j]; > > > > } > > > > } > > > > > > // --------------- > > // Copy the scores > > // --------------- > > > > for (var i:Number = 0; i < 2; i++) { > > > > score = new Array(); > > > > for (var j:Number = 0; j < winPlaces; j++) { > > > > score[i][j] = state.score[i][j]; > > numPieces = state.numPieces; > > } > > > > } > > > > > > } > > > > AS2 seems not to like this as I get the following error > > > > A class must have only one constructor. > > public function Connect4State(state:Connect4State) > > > > Anyone suggest an alternative to this "Copy Constructor"? > > > > > > > > > > > > -----Original Message----- > > From: [EMAIL PROTECTED] > > [mailto:[EMAIL PROTECTED] On Behalf Of Paul > > Steven > > Sent: 02 November 2006 20:01 > > To: 'Flashcoders mailing list' > > Subject: [Flashcoders] Copy Constructor > > > > Hi there > > > > Is there an equivalent of the Java Copy constructor in Flash AS2? > > > > Thanks > > > > Paul > > > > _______________________________________________ > > Flashcoders@chattyfig.figleaf.com > > To change your subscription options or search the archive: > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > > > Brought to you by Fig Leaf Software > > Premier Authorized Adobe Consulting and Training > > http://www.figleaf.com > > http://training.figleaf.com > > > > _______________________________________________ > > Flashcoders@chattyfig.figleaf.com > > To change your subscription options or search the archive: > > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > > > Brought to you by Fig Leaf Software > > Premier Authorized Adobe Consulting and Training > > http://www.figleaf.com > > http://training.figleaf.com > > _______________________________________________ > Flashcoders@chattyfig.figleaf.com > To change your subscription options or search the archive: > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > Brought to you by Fig Leaf Software > Premier Authorized Adobe Consulting and Training > http://www.figleaf.com > http://training.figleaf.com > > _______________________________________________ > Flashcoders@chattyfig.figleaf.com > To change your subscription options or search the archive: > http://chattyfig.figleaf.com/mailman/listinfo/flashcoders > > Brought to you by Fig Leaf Software > Premier Authorized Adobe Consulting and Training > http://www.figleaf.com > http://training.figleaf.com _______________________________________________ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com